简体   繁体   English

从上一个活动获取文本并将其添加到另一个活动列表视图

[英]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). 使您的类实现ParcelableSerializable (较慢)。

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.

相关问题 存储要从另一个活动调用的活动列表的最佳方式 - Best way to store list of activities to be called from another activity 将文本从一个活动添加到另一个活动的错误 - Bug at adding text from one activity to another 使用按钮从另一个活动中获取文本 - getting text from another activity using button Android:从另一个活动设置文本视图的文本不起作用 - Android: Set Text View's text from another activity Not working 通过单击另一个活动中的阵列列表中的项目将项目添加到阵列列表 - Adding items to an array list by clicking on an item from an arraylist in another activity 单击另一个活动的按钮后修改文本视图值 - Modify Text View values after clicking a button from another activity 通过单击列表视图中创建的卡片,从一项活动转移到另一项活动 - Moving from one activity to another by click on card created in a list view 如何从第一个活动的文本视图中获取数据并从另一个活动中发布数据? - How to get data from text view in first activity and post it from another activity? 我有一个列表视图。如何将列表视图的点击总数保存到另一个活动文本视图? - I have a list view.how i will save list view total number of click to another activity text view? 以完整的先前活动数据开始活动 - Start activity with previous activities data intact
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM