【问题标题】:ListView using a SimpleAdapter is not updating with new data使用 SimpleAdapter 的 ListView 未使用新数据进行更新
【发布时间】:2014-09-03 08:36:10
【问题描述】:

我将数据从我的第二个活动传递到第一个活动 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();

【问题讨论】:

  • 请避免在您的问题标题前加上Android之类的标签名称,底部的标签足以显示问题的范围。

标签: android android-intent android-listview simpleadapter


【解决方案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();

【讨论】:

    【解决方案2】:

    您需要在 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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      相关资源
      最近更新 更多