ArrayAdapter无法正常工作 - Android

时间:2013-08-22 20:17:06

标签: android arraylist android-asynctask android-arrayadapter

我有一个扩展ArrayAdapter的类。这个类有效,但问题是当我向List添加一个项目时它会加倍该项目,所以有两个相同的列表项。这是我当前的ArrayAdapter类,

class Item {
                String username;
                String number;
                String content;

                public Item (String username, String number, String content){
                     this.username = username;
                     this.number = number;
                     this.content = content;
                     }

                public CharSequence getNumber() {
                    // TODO Auto-generated method stub
                    return username;
                }

                public CharSequence getUsername() {
                    // TODO Auto-generated method stub
                    return number;
                }

                public CharSequence getContent() {
                    // TODO Auto-generated method stub
                    return content;
                }

            }


             class ListAdapter extends ArrayAdapter<Item> {

                public ListAdapter(Context context, int textViewResourceId) {
                    super(context, textViewResourceId);
                    // TODO Auto-generated constructor stub
                }

                private List<Item> items;

                public ListAdapter(Context context, int resource, List<Item> items) {

                    super(context, resource, items);

                    this.items = items;

                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    View v = convertView;

                    if (v == null) {

                        LayoutInflater vi;
                        vi = LayoutInflater.from(getContext());
                        v = vi.inflate(R.layout.list_item, null);

                    }

                    final Item p = items.get(position);

                    if (p != null) {


                        TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
                        TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
                        Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
                       Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

                        commentView.setText(p.getContent());
                        NumbersView.setText(p.getNumber());
                        usernamesView.setText(p.getUsername());




                       usernameButton.setOnClickListener(new View.OnClickListener() {

                           public void onClick(View view) {
                               Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
                               i.putExtra("usernameOfProfile", p.username);
                               startActivity(i);
                               finish();
                           }
                       });

                       numberButton.setOnClickListener(new View.OnClickListener() {

                           public void onClick(View arg0) {
                               Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
                               i.putExtra("NumberProfile", p.number);
                               startActivity(i);
                               finish();
                           }
                       });
                    }

                    return v;

                }
             }

             final List <Item> tempList = new ArrayList <Item>();

                ListView yourListView = (ListView) findViewById(android.R.id.list);


                final ListAdapter customAdapter = new ListAdapter(DashboardActivity.this, R.layout.list_item, tempList);

                yourListView.setAdapter(customAdapter);

这是我的AsyncTask,它在后台收集信息。在onPostExecute中我将Items添加到我的列表中。

final ProgressDialog progDailog = new ProgressDialog(DashboardActivity.this);
            class loadComments extends AsyncTask<JSONObject, String, JSONObject> {



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


                    progDailog.setIndeterminate(false);
                    progDailog.setCancelable(true);
                    progDailog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    progDailog.show();
                    progDailog.setContentView(R.layout.progress_circle);


                } 

                @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){ 



                                String username = json2.getString(KEY_USERNAME);
                                String number = json2.getString(KEY_NUMBER);
                                String content = json2.getString(KEY_COMMENT);
                                tempList.add (new Item (username, number, content));
                                customAdapter.addAll(tempList);



                                }//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    
                    progDailog.dismiss();

                }


            }

            new loadComments().execute();

1 个答案:

答案 0 :(得分:0)

在执行addAll

之前,尝试清除适配器的内容