使用SimpleAdapter的ListView不会使用新数据进行更新

时间:2014-09-03 08:36:11

标签: android android-intent android-listview simpleadapter

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

这是我的代码

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

2 个答案:

答案 0 :(得分: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 :(得分: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
        }
    }
}