[英]Getting text from previous activity and adding it another activities list view
How do I add items to a list dynamically when button is clicked? 单击按钮后如何动态地将项目添加到列表中? The text is added to the
listView
. 文本被添加到
listView
。 At the moment it only adds one item to the list view, and when you try to add another it just changes the already added item in the list. 目前,它仅将一个项目添加到列表视图,而当您尝试添加另一个项目时,它仅更改列表中已添加的项目。
Here is my code: 这是我的代码:
This is the button on click for activity A that gets the text: 这是活动A的单击按钮,它获取文本:
schedule.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
AddSchedule();
Intent i = new Intent(ScheduleActivity.this, MainActivity.class);
String str = event_name.getText().toString();
i.putExtra("myExtra", str);
startActivity(i);
}
});
Then in activity B it gets the text from activity B but only adds the 然后在活动B中从活动B获取文本,但仅添加
Final ListView listView = (ListView) findViewById(R.id.listView); 最终ListView listView =(ListView)findViewById(R.id.listView); adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter);
listView.setAdapter(适配器);
Intent i = getIntent();
if (i.hasExtra("myExtra")) {
list.add(i.getStringExtra("myExtra"));
adapter.notifyDataSetChanged();
}
You only see one item in the new activty's list, since that Activity is created each time via startActivity(Intent) (I suppose). 您只能在新活动的列表中看到一项,因为该活动是每次通过startActivity(Intent)创建的(我想)。 So each time open Activity B, it's a new instance.
因此,每次打开活动B时,它都是一个新实例。 The arraylist is empty.
arraylist为空。
You need to keep track of the items clicked in Activity A and send the whole list to Activity B, every time. 您需要跟踪活动A中单击的项目,并每次将整个列表发送到活动B。 Activity B should get the complete list in the intent and create the gui-list.
活动B应该获得意图中的完整列表并创建gui列表。
Make your class implement Parcelable or Serializable
(slower). 使您的类实现Parcelable或
Serializable
(较慢)。
Inside activity B you should include the following: 在活动B中,您应包括以下内容:
@Override
public void onSaveInstanceState(Bundle out){
super.onSaveInstanceState(out);
out.putStringArrayList(key, list);
}
"key" is a final String you have defined in Activity's B member variables. “键”是您在Activity的B成员变量中定义的最终String。
This will make sure your list is saved before being destroyed (For more, read about Activity Lifecycle . Now, you also need to restore in whenever the activity is created. You can do that in onCreateView
like so: 这将确保您的列表在销毁之前已保存(有关更多信息,请参见Activity Lifecycle 。现在,无论何时创建活动,您都还需要还原。您可以在
onCreateView
执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//restore the contents of your list
if (savedInstanceState!=null)
list=savedInstanceState.getStringArrayList(key);
/* initialize the adapter and set it to the list */
//also add the new item from activity A
Intent i = getIntent();
if (i.hasExtra("myExtra")) {
list.add(i.getStringExtra("myExtra"));
adapter.notifyDataSetChanged();
}
}
General note, instead of adding to the list and afterwards notifying the adapter, you can instead add your items directly to the adapter. 一般说明,您可以将项目直接添加到适配器中,而不必添加到列表中并随后通知适配器。 The adapter will automatically update your list.
适配器将自动更新您的列表。
Edit: Yes, your list should be a member variable because it needs to be accessed in 2 methods. 编辑:是的,您的列表应该是一个成员变量,因为它需要通过2种方法进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.