0

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;
                    }
LimpLimp
  • 37
  • 2
  • 11

3 Answers3

0

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()
bGorle
  • 1,978
  • 2
  • 22
  • 28
  • We'll i want the listview to keep entries like a notepad app list.clear seems to erase the entry. I may have placed it in the wrong place currently its right after setContentView(R.layout.activity_argue_list); – LimpLimp Nov 19 '13 at 17:40
  • It depends on how you are loading the data. With out seeing your code i can't tell you much. This works for me. you can only know where to place the thing. It will definitely works.. – bGorle Nov 21 '13 at 05:12
  • ya i understood...you are sending each row at time...it will add a single row to the list – bGorle Nov 21 '13 at 14:44
  • but why its duplicating the previous entry is where Im lost at. Doesnt make any sense. – LimpLimp Nov 21 '13 at 14:47
  • duplicating the new entry i mean – LimpLimp Nov 21 '13 at 14:51
  • Dude you are using this one.. 'list.add(0, Lrg);' may be this is the reason...always It is taking the 0th one..Just try list.add(lrg); – bGorle Nov 21 '13 at 14:51
  • I thought that may have been it as well. I've made a few changes to that line any ideas on what to change it to? – LimpLimp Nov 21 '13 at 14:55
  • sorry had to make a few other changes to the code. It populates but does the same thing after removing the 0 – LimpLimp Nov 21 '13 at 15:58
0

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. :)

Community
  • 1
  • 1
Basavaraju B V
  • 174
  • 1
  • 8
0

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

AnupamChugh
  • 1,779
  • 1
  • 25
  • 36