Android ListView Duplicates entries

China☆狼群 提交于 2019-12-23 05:32:15

问题


If i type "Hello" in my edittext in activity 1 it will add "Hello" to the listview in activity 2 as the first entry ( which has no error ). Also, if I type "Apple" as my second entry I will change the first entry of "Hello" to "Apple" and add the new 2nd entry "Apple"... causing both entries to become "Apple" and "Apple"... any thoughts?

Act2 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_argue_list);

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


    Bundle bun = getIntent().getExtras();
     Lrg = bun.getString("Jenny");
     Med = bun.getString("Forest");
     Med2 = bun.getString("Gump");

        KoreyzAdapter add = new KoreyzAdapter();
        txtlist.setAdapter(add);
        list.add(0, Lrg);
        add.notifyDataSetChanged();
        setListAdapter(add);
}
class KoreyzAdapter extends ArrayAdapter<String>{

      public KoreyzAdapter() {
          super(ArgueListActivity.this, android.R.layout.simple_list_item_1, list);
    }
      class ViewHolder{
          TextView who;
          TextView sex;
          TextView cat;
          ImageView pic;

          ViewHolder(View v){
              who = (TextView) v.findViewById(R.id.inputLrgtxt);
              sex = (TextView) v.findViewById(R.id.whoMedtxt);
              cat = (TextView) v.findViewById(R.id.stupidMedtxt);
              pic = (ImageView)v.findViewById(R.id.sexpic);
          }

      }
                public View getView(int position, View convertView, ViewGroup parent) {
                    View row = convertView;
                    ViewHolder holder = null;
                    if(row == null){

                         LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        row = inflator.inflate(R.layout.list_item, parent, false);
                        holder = new ViewHolder(row);
                        row.setTag(holder);

                    }else{
                        holder = (ViewHolder)row.getTag();
                    }

                        holder.who.setText(Lrg);
                        holder.sex.setText(Med);
                        holder.cat.setText(Med2);

                        holder.pic.setImageResource(R.drawable.maleandfemale);

                        return row;
                    }

回答1:


You have to clear the adapter data. Before loading the listView from adapter make the list clear at onCreate() of your activity. It is just appending the old data. That's why it is showing two times. I hope this will work.

Use this one onCreate():

list.clear()



回答2:


Check this link. It might help you as well.

Even I was facing the same problem it took me 2 days to get rid of that issue.

I hope this helped you. :)




回答3:


With ArrayAdapters always pass the layout of the list row items in the constructor.

Example : TodosAdapter(Context context, int layoutResourceId, List<Todo> todos);
int layoutResourceId is android.R.layout.simple_list_item_1



来源:https://stackoverflow.com/questions/20052735/android-listview-duplicates-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!