[英]Android: ListView is not updating when i passed data from another activity
I am new to android and i am making listview based app. 我是Android新手,正在制作基于Listview的应用程序。 In my app i have items from Acitity_2 to Activity_1. 在我的应用程序中,我有从Acitity_2到Activity_1的项目。 Activity_1 has the list view. Activity_1具有列表视图。 when user click on ADD button of activity1 then Activity_2 will be called. 当用户单击活动1的添加按钮时,将调用活动_2。 Activity_2 has the EditText. Activity_2具有EditText。 The value of EditText should update the ListView. EditText的值应更新ListView。 But it only takes last value of the Edittext. 但是它仅采用Edittext的最后一个值。
Here is the code of Activity_2: 这是Activity_2的代码:
Intent intent = new Intent(ItemDetails.this, MainActivity.class);
intent.putExtra("values", values);
startActivity(intent);
Code for Activity_1 活动_1的代码
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
protected Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Setting a custom layout for the list activity */
setContentView(R.layout.activity_main);
/** Reference to the add button of the layout main.xml */
Button btn = (Button) findViewById(R.id.btnAdd);
/** Reference to the delete button of the layout main.xml */
Button btnDel = (Button) findViewById(R.id.btnDel);
/** Defining the ArrayAdapter to set items to ListView */
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
list.clear();
list.add("Aluminium foil");
/** Setting the adapter to the ListView */
setListAdapter(adapter);
String val="";
if(getIntent().getStringExtra("values")!=null && getIntent().getStringExtra("values").length() > 0){
val = getIntent().getStringExtra("values");
list.add(val);
}
adapter.notifyDataSetChanged();
Now its overriding last value of edittext. 现在,它覆盖了edittext的最后一个值。 Suggestions are appreciated. 建议表示赞赏。
Any good ?? 有什么好处吗??
Set adapter first time as 将适配器第一次设置为
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
listView.setAdapter(adapter);
on coming back from Activity 2 从活动2回来时
String val="";
if(getIntent().getStringExtra("values")!=null && getIntent().getStringExtra("values").length > 0){
val = getIntent().getStringExtra("values");
list.add(val);
}
adapter.notifyDataSetChanged();
you basically dont have to create a new adapter object each time you come back 您基本上不必每次回来都创建新的适配器对象
You should start second Activity with startActivityForResult. 您应该使用startActivityForResult开始第二个Activity。 Then insert the value in the editext and finishing the activity sending back the result. 然后将值插入editext并完成活动,将结果发送回去。 In activity1 intercept the result in the methos onActivityResult and update the listview. 在activity1中,在方法onActivityResult中截取结果并更新列表视图。
Check out this link about activity results: http://developer.android.com/training/basics/intents/result.html 查看有关活动结果的此链接: http : //developer.android.com/training/basics/intents/result.html
为了让Android更新您的listview,您需要在Adapter
上调用notifyDataSetChanged()
。
It's not enough to simply update your data set, you have to let the Adapter
know so it reads the data again and updates the list. 仅更新数据集是不够的,您必须让Adapter
知道,以便Adapter
再次读取数据并更新列表。 I can't see what your adapter is called from your code, but just call notifyDataSetChanged()
on your adapter and it should work. 我看不到您的适配器从您的代码中调用了notifyDataSetChanged()
,但是只需在您的适配器上调用notifyDataSetChanged()
。
有很多方法可以实现……但是最好的最简单的解决方案是使用:refreshDisplay();
Do this, 做这个,
String itemVal=getIntent().getStringExtra("values");
if(itemVal==null){
}else{
list.add(itemVal);
}
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.