2014-09-03 39 views
0

我将第二个活动的数据传递给第一个活动ListView。当我将数据从第二个Activity传递到第一个Activity时,ListView项目每次都会覆盖。我想每次添加新项目。我正在使用SimpleAdapter。但它不会更新新项目。使用SimpleAdapter的ListView没有用新数据更新

这里是我的代码

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

这里是从那里我传递的价值观

  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

请避免在您的问题标题前添加标签名称,比如Android,底部的标签足以显示问题的范围。 – Luksprog 2014-09-03 08:59:38

回答

1

每次返回的第一个活动,你创建一个新的适配器只有保持时间的代码新项目,所以你会覆盖任何以前的数据。相反,你应该刚刚更新,你的适配器基于添加新项目的初始列表:

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

您需要创建您的onActivityResult()以外的名单,因为每次你创建新的列表和你的旧数据将会消失。

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); 
       HashMap<String, String> hm = new HashMap<String,String>(); 
       hm.put("txt", strName); 
       hm.put("cur",strQty); 
       aList.add(hm); 
      adapter.notifyDataSetChanged(); 

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