Android 使用SimpleAdapter的ListView未使用新数据更新
我正在将数据从第二个活动传递到第一个活动列表视图。当我将数据从第二个活动传递到第一个活动时,每次都会覆盖ListView项。我想每次都添加新项目。我正在使用SimpleAdapter。但它并没有更新新的项目 这是我的密码Android 使用SimpleAdapter的ListView未使用新数据更新,android,android-intent,android-listview,simpleadapter,Android,Android Intent,Android Listview,Simpleadapter,我正在将数据从第二个活动传递到第一个活动列表视图。当我将数据从第二个活动传递到第一个活动时,每次都会覆盖ListView项。我想每次都添加新项目。我正在使用SimpleAdapter。但它并没有更新新的项目 这是我的密码 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if(re
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
}
}
}
每次返回到第一个活动时,您都会创建一个仅包含新项的新适配器,以便覆盖任何以前的数据。相反,您只需更新适配器所基于的初始列表即可添加新项:
//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();
//我假设当您第一次创建SimpleAdapter时
//将名为mData的哈希映射列表传递给它(这通常是活动中的一个字段)
//然后在onActivityResult()中,您将有:
字符串strName=data.getStringExtra(“名称”);
字符串strQty=data.getStringExtra(“数量”);
//创建包含新项目的新地图
HashMap hm=新的HashMap();
hm.put(“txt”,strName);
hm.put(“当前”,strQty);
//将新映射添加到适配器所基于的列表中
mData.add(hm);
//更新适配器
adapter.notifyDataSetChanged();
您需要在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
}
}
}
activityresult上受保护的void(int-requestCode、int-resultCode、Intent-data){
if(requestCode==1){
if(resultCode==RESULT\u OK){
Bundle b=data.getExtras();
如果(b!=null){
字符串strName=data.getStringExtra(“名称”);
字符串strQty=data.getStringExtra(“数量”);
System.out.println(strName);
系统输出打印项次(strQty);
HashMap hm=新的HashMap();
hm.put(“txt”,strName);
hm.put(“当前”,strQty);
添加(hm);
adapter.notifyDataSetChanged();
}
}
if(resultCode==RESULT\u取消){
//如果没有结果,请编写代码
}
}
}
请避免在问题标题前加上Android等标记名,底部的标记足以显示问题的范围。
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
}
}
}