Android : Android: ListView is not updating with new data using SimpleAdapter

on Wednesday, September 3, 2014


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();

0 comments:

Post a Comment