Android ListView Duplicates entries

2019-06-09 12:09发布

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

3条回答
在下西门庆
2楼-- · 2019-06-09 12:55

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楼-- · 2019-06-09 12:57

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

查看更多
狗以群分
4楼-- · 2019-06-09 13:06

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()
查看更多
登录 后发表回答