0

我有一个异步任务,它使用 JSON 方法收集评论、用户名和数字。然后我有一个扩展 BaseAdapter 的类,假设将评论和用户名放入 listView。问题是如何获得 BaseAdapter 类的评论、用户名和数字?这是我当前的代码

   class loadComments extends AsyncTask<JSONObject, String, JSONObject> {

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


        } 

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

        } 

        protected JSONObject doInBackground(JSONObject... params) {


            JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                return json2;



        }

        @Override
        protected void onPostExecute(JSONObject json2) {
            try {  
                if (json2.getString(KEY_SUCCESS) != null) { 
                    registerErrorMsg.setText("");
                    String res2 = json2.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res2) == 1){ 
                         l1=(ListView)findViewById(R.id.list);


                        JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
                        String comments[] = new String[commentArray.length()];
                        for ( int i=0; i<commentArray.length(); i++ ) {
                            comments[i] = commentArray.getString(i);
                        }
                        JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
                        String numbers[] = new String[numberArray.length()];
                        for ( int i=0; i<numberArray.length(); i++ ) {
                            numbers[i] = numberArray.getString(i);
                        }
                        JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
                        String usernames[] = new String[usernameArray.length()];
                        for ( int i=0; i<usernameArray.length(); i++ ) {
                            usernames[i] = usernameArray.getString(i);
                        }











                        }//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    
        }
    }


    new loadComments().execute();

     class CreateCommentLists extends BaseAdapter{
        Context ctx_invitation;
        String[] listComments;
        String[] listNumbers;
        String[] listUsernames;


        public CreateCommentLists(Context ctx_invitation, String[] comments, String[] Numbers, String[] usernames)
        {
            super();
            this.ctx_invitation = ctx_invitation;
            listComments = comments;
            listNumbers = Numbers;
            listUsernames = usernames;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listComments.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return listComments[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = null;
            try
            {
                String inflater = Context.LAYOUT_INFLATER_SERVICE;
                LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
                v = li.inflate(R.layout.list_item, null);

                TextView commentView = (TextView)v.findViewById(R.id.listComment);
                TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
                TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);


                commentView.setText(listComments[position]);
                NumbersView.setText(listNumbers[position]);
                usernamesView.setText(listUsernames[position]);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return v;
        }

     }
4

2 回答 2

0

覆盖您已实现的适配器的值或为它们添加设置器。然后设置值并确保调用notifyDatasetChanged适配器。

如果适配器是全局变量,您可以直接引用它,也可以在 AsyncTask 的构造函数中传递它

于 2013-07-13T16:24:35.870 回答
0

onPostExecute您可以使用新数据创建一个新数据,然后使用setAdapterBaseAdapter将其设置为您的ListView. 或者,如前所述,您可以使用 setter 更新当前适配器中的数据,然后通知ListView它应该重绘自己。

通过 AsyncTask 的本质,你可以在任何你想要的地方更新你的 UI onPreExecutiononProgressUpdate并且onPostExecution.

于 2013-07-15T08:28:28.053 回答