简体   繁体   English

Android:当我从其他活动传递数据时,ListView没有更新

[英]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.

相关问题 从另一个活动android传递的HashMap数据填充listView - fill listView from a HashMap data passed from another activity, android Android:Listview数据未在其他活动中传递 - Android : Listview data is not being passed in another activity 从另一个活动更新ListView - Updating ListView from another Activity 如何将数据放在listView上,并通过另一个活动? - How to put data on listView,passed by another activity? Android:将数据从一个活动传递到ListView中的另一个活动 - Android: Passing data from one activity to another activity in ListView LiveData 不会将数据从一个活动更新到另一个活动 - Android - LiveData not updating data from one Activity to another Activity - Android 数据未从列表视图传递到活动中 - Data not being passed from listview into activity 从另一个Activity更新ListView的项目 - Updating an item of ListView from another Activity 当我从另一个活动获得结果时,ListView仅更新一个视图项,如何获得所有过去的结果? - ListView is updating only one view item when I get the results from another activity, how do I get all the past results? 我想将listview数据发送到android中的另一个活动 - I want to send listview data to another activity in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM