-2

i've created a custom list with four textviews...the data in this list is saved through a dialog which has a ok button. when i add the data, it gets saved in the list(works fine till now). when i add the next element, all the rows gets same value as the last one...the notifyDatasetChanged() is also not working...am i wrong somewhere?...this is my code...

ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   String val =  editquantity.getText().toString();
                    valq1 = Integer.parseInt(val);
                    ListView l;
                    l = (ListView) v.findViewById(R.id.order_listview);
                    myAdapter adapter = new myAdapter(getActivity(), row);
                    l.setAdapter(adapter);
                    row.add("");
                    adapter.notifyDataSetChanged();
                    builder.dismiss();

                }
            });

            builder.setView(dialog);
            builder.show();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

2

You created new adapter everytime you click ok. Put your adapter code outside the onclick method:

ListView l = (ListView) v.findViewById(R.id.order_listview);
myAdapter adapter = new myAdapter(getActivity(), row);
l.setAdapter(adapter);

ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String val =  editquantity.getText().toString();
                valq1 = Integer.parseInt(val);

                row.add(""); //add new item to the list
                adapter.notifyDataSetChanged(); //notify adapter
                builder.dismiss();

            }
        });

        builder.setView(dialog);
        builder.show();
klaudiusnaban
  • 196
  • 1
  • 10
1

You need to use addAll() method for adding new values remaining old values are same in arraylist.

row.addAll("test");
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19
0

Why are you adding row as blank. Secondly you have set row in adapter without defining it. Please provide more details to the question as i cannot understand where row has been declared. Coming to the similar value problem. You need to update row array because it has been set on adapter.

0

There are three things you need to change 1. Declare your ListView object outside of the onCreate() 2. Declare your adapter object outside of the onCreate()
Now this initialization of this below line needs to be initialized only once in onCreate(). You need not to initialize it again and again when you add new item.So below line write in your onCreate().

myAdapter adapter = new myAdapter(getActivity(), row);

When you add new item you only need to add that item in your arraylist named row onClick of ok button and then call adapter.notifyDataSetChanged(); it will work fine.

Hiren Shah
  • 37
  • 11