0

doInBackground() で json 関数を使用する asynctask があり、変数を onPostExecute に返します。問題は、json 関数を while ループにしたいということです。ループに入るたびに、コメントが収集されます。したがって、15 個のコメントを持つことができ、それらすべてを onPostExecute に送信してビューに配置したいと考えています。これが私のコードです

     class loadComments extends AsyncTask<JSONObject, String, List<String>> {
             DatabaseHandler db = new DatabaseHandler(getApplicationContext());





            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

            }


            protected List<String> doInBackground(JSONObject... params) {
            //do your work here
                List<String> results = new ArrayList<String>();
             int i = 0;
                while (i < 5 ){
                JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
                result = json2;
                results.add(result);
                i++;
                }


                return results;

            }

            @Override
            protected void onPostExecute(List<String> results) {
                try {  
                    if (json2.getString(KEY_SUCCESS) != null) { 
                        registerErrorMsg.setText("");
                        String res2 = json2.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res2) == 1){ 

                                commentView.setText(json2.getString(KEY_COMMENT));
                                    LinearLayout.LayoutParams commentViewParams = new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    commentViewParams.setMargins(20, 10, 20, 20);
                                    commentView.setBackgroundResource(R.drawable.comment_bg);
                                     commentView.setTextColor(getResources().getColor(R.color.black)); 
                                    commentBox.addView(commentView, commentViewParams);





                            }//end if key is == 1
                        else{
                            // Error in registration
                            registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                        }//end else
                    }//end if
                } //end try

                catch (JSONException e) { 
                    e.printStackTrace();
                }//end catch    
            }
        }

このコードは機能しませんが、私が達成しようとしていることの例です

4

1 に答える 1

0

1. json データを解析し、必要なすべての変数をグローバル変数として初期化する関数を作成します。 2. パラメータを渡さずに、AsyncTask の doInBackground() メソッド内で関数を実行します。 3. onPostExecute からビューに値を設定します。 () 方法

于 2013-07-05T17:56:53.757 に答える