【问题标题】:Android: ListView is not updating when i passed data from another activityAndroid:当我从另一个活动传递数据时,ListView 没有更新
【发布时间】:2014-07-23 23:19:36
【问题描述】:

我是 android 新手,我正在制作基于列表视图的应用程序。在我的应用程序中,我有从 Acitity_2 到 Activity_1 的项目。 Activity_1 具有列表视图。当用户单击activity1的添加按钮时,将调用Activity_2。 Activity_2 有 EditText。 EditText 的值应该更新 ListView。但它只需要 Edittext 的最后一个值。

这里是Activity_2的代码:

          Intent intent = new Intent(ItemDetails.this, MainActivity.class);
          intent.putExtra("values", values);
          startActivity(intent);

Activity_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();

现在它覆盖了 edittext 的最后一个值。 欢迎提出建议。

【问题讨论】:

    标签: android android-layout android-intent android-activity android-listview


    【解决方案1】:

    有什么好处吗??

    将适配器首次设置为

    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
    listView.setAdapter(adapter);
    

    从活动 2 回来

     String val="";
             if(getIntent().getStringExtra("values")!=null &&    getIntent().getStringExtra("values").length > 0){
        val = getIntent().getStringExtra("values");
        list.add(val);
            }                    
    
    adapter.notifyDataSetChanged();
    

    你基本上不必每次回来都创建一个新的适配器对象

    【讨论】:

    • 谢谢,但我也试过了,但这不起作用。我还在问题中编辑了我的代码。你可以检查一下..非常感谢所有的帮助。
    • 每次从活动 B 回到活动 A 时,您都在创建一个新列表。正确的做法是如前一条评论中所建议的那样。使用 startActivityForResult 启动第二个活动 B 并设置返回意图中的edittext值..接收onActivityResult中的值..将该值添加到列表中..调用notifyDataSetChanged
    【解决方案2】:

    您应该使用 startActivityForResult 启动第二个 Activity。然后在编辑文本中插入值并完成发送回结果的活动。在activity1中截取onActivityResult方法中的结果并更新listview。

    查看有关活动结果的链接: http://developer.android.com/training/basics/intents/result.html

    【讨论】:

      【解决方案3】:

      要让 android 更新您的列表视图,您需要在您的 Adapter 上调用 notifyDataSetChanged()

      【讨论】:

        【解决方案4】:

        仅仅更新您的数据集是不够的,您必须让Adapter 知道,以便它再次读取数据并更新列表。我无法从您的代码中看到您的适配器是什么,但只需在您的适配器上调用 notifyDataSetChanged(),它应该可以工作。

        【讨论】:

        • 感谢您的回复,但我想让您知道我使用了 notifuDataSetChanged(),如下所示
        • 感谢您的回复,但我想让您知道我已经使用了 notifyDatasetChanged 方法。这是代码: /** 定义 ArrayAdapter 以将项目设置为 ListView */ adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list); list.add("铝箔"); adapter.notifyDataSetChanged();
        【解决方案5】:

        这样做,

        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-28
          • 2016-03-11
          • 2015-12-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多