0

У меня есть класс, который расширяет ArrayAdapter. Этот класс работает, но проблема заключается в том, что когда я добавляю элемент в список, он удваивает этот элемент, поэтому есть два одинаковых элемента списка. Вот мой текущий класс ArrayAdapter,ArrayAdapter не работает должным образом - Android

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 я добавляю элементы в свой список.

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(); 
+0

TempList выглядит хитроумный ... буду иметь более близкий взгляд позже –

ответ

0

Постарайтесь, чтобы очистить содержимое адаптера, прежде чем делать addAll

Смежные вопросы