Search code examples
androidandroid-intentandroid-listviewsimpleadapter

ListView using a SimpleAdapter is not updating with new data


I am passing the data from my second activity to first activity ListView. When i am passing the data from second Activity to first Activity ListView items is overriding each time. I want to add new item each time. I am using SimpleAdapter. But it is not updating with new items.

Here is my code

         protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                Bundle b = data.getExtras();
                if ( b!= null ){
                String strName=data.getStringExtra("name");
                String strQty=data.getStringExtra("quantity");
                System.out.println(strName);
                System.out.println(strQty);
                List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();


                    HashMap<String, String> hm = new HashMap<String,String>();

                    hm.put("txt", strName);
                    hm.put("cur",strQty);
                    aList.add(hm);

                // Keys used in Hashmap
                String[] from = {"txt","cur" };

                // Ids of views in listview_layout
                int[] to = {R.id.txt,R.id.cur};

                // Instantiating an adapter to store each items
                // R.layout.listview_layout defines the layout of each item
                adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.add_list_detail, from, to);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

                }
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }

And here is the code from where i am passing the values

          EditText editName = (EditText) findViewById(R.id.txtName);
             EditText editQty=(EditText) findViewById(R.id.txtqty);
             String name= editName.getText().toString();
             String quantity=editQty.getText().toString();
             Intent returnIntent = new Intent();
             returnIntent.putExtra("name",name);
             returnIntent.putExtra("quantity",quantity);
              setResult(RESULT_OK,returnIntent);
             finish();

Solution

  • Every time you return to the first activity you create a new adapter holding only the new item so you'll override any previous data. Instead, you should just update the initial list on which your adapter is based to add the new item:

    //I'm assuming that when you first create the SimpleAdapter
    // you pass to it a List of HashMaps named mData(this would normally be a field in your activity)
    // then in the onActivityResult() you'd have:
    String strName=data.getStringExtra("name");
    String strQty=data.getStringExtra("quantity");
    // create a new map holding the new item
    HashMap<String, String> hm = new HashMap<String,String>();
    hm.put("txt", strName);
    hm.put("cur",strQty);
    // add the new Map to the list on which the adapter is based
    mData.add(hm);
    // update the adapter
    adapter.notifyDataSetChanged();