【问题标题】:android ArrayAdapter items updateandroid ArrayAdapter 项目更新
【发布时间】:2012-02-21 15:56:22
【问题描述】:

我有这个项目结构的 ArrayAdapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >

         <TextView
             android:id="@+id/itemTextView"
             ... />
</RelativeLayout>

然后添加这个适配器:

mAdapter = new ArrayAdapter<String>(this, R.layout.item, 
                                            R.id.itemTextView, itemsText);

一切都很好,但我想更新适配器项目中的文本。我找到了解决办法

mAdapter.notifyDataSetChanged();

但不明白如何使用它。请帮忙。

更新 我的代码:

String[] itemsText = {"123", "345", "567"};
ArrayAdapter<String> mAdapter;

onCreate

mAdapter = new ArrayAdapter<String>(this, R.layout.roomitem, 
                                              R.id.itemTextView, itemsText);
setListAdapter(mAdapter);
itemsText = {"789", "910", "1011"};

点击

mAdapter.notifyDataSetChanged();
//it's dont work

【问题讨论】:

    标签: android android-arrayadapter


    【解决方案1】:

    我觉得是这样的

    public void updatedData(List itemsArrayList) {
    
        mAdapter.clear(); 
    
        if (itemsArrayList != null){
    
            for (Object object : itemsArrayList) {
    
                mAdapter.insert(object, mAdapter.getCount());
            }
        }
    
        mAdapter.notifyDataSetChanged();
    
    }
    

    【讨论】:

    • 您不需要向适配器添加项目,只需在数组列表上完成工作后调用 notifyDataSetChanged
    • 我调用 notifyDataSetChanged 但没有任何反应
    • 你在 onClick 中做了什么,或者你在 itemsText 中更改某些内容的方法?
    • 这样做会导致列表中的所有项目在每次发生更改时闪烁,并且列表视图每次都会滚动回顶部。不要这样做。
    【解决方案2】:

    您的问题是典型的带有指针的 Java 错误。

    第一步,您将创建一个数组并将该数组传递给适配器。

    在第二步中,您将使用新信息创建一个新数组(因此创建了新指针),但适配器仍指向原始数组。

    // init itemsText var and pass to the adapter
    String[] itemsText = {"123", "345", "567"};
    mAdapter = new ArrayAdapter<String>(..., itemsText);
    
    //ERROR HERE: itemsText variable will point to a new array instance
    itemsText = {"789", "910", "1011"};
    

    所以,你可以做两件事,一是更新数组内容而不是创建一个新的:

    //This will work for your example
    items[0]="123";
    items[1]="345";
    items[2]="567";
    

    ...或者我会做什么,使用列表,例如:

    List<String> items= new ArrayList<String>(3);
    boundedDevices.add("123");
    boundedDevices.add("456");
    boundedDevices.add("789");
    

    在更新中:

    boundedDevices.set("789");
    boundedDevices.set("910");
    boundedDevices.set("1011");
    

    要添加更多信息,在实际应用程序中,您通常会使用来自服务或内容提供者的信息更新列表适配器的内容,因此通常要更新项目,您会执行以下操作:

    //clear the actual results
    items.clear()
    
    //add the results coming from a service
    items.addAll(serviceResults);
    

    这样您将清除旧结果并加载新结果(认为新结果应该有不同数量的项目)。

    当然在更新数据后调用notifyDataSetChanged();

    如果您有任何疑问,请随时发表评论。

    【讨论】:

    • 我正在使用片段。我完成了上述所有步骤。但是,我的 arrayadapter 没有更新。任何建议
    • 请举个例子。
    【解决方案3】:

    假设 itemTexts 为 String 数组或 String ArrayList,你在其中添加新项目到 itemsText 之后你可以调用

    mAdapter.notifyDataSetChanged();
    

    如果您没有得到答案,请输入一些代码。

    【讨论】:

      【解决方案4】:

      我做了这样的事情。它工作正常。

      向适配器类添加方法:

      public void updateList(ArrayList<ITEM> itemList){
              this.itemList.clear();
              this.adapterList = new ArrayList<ITEM>();
              this.adapterList .addAll(itemList);
      
              notifyDataSetChanged();
          }
      

      调用你使用适配器的类中的方法:

      itemList.add(item);
      adapter.updateList(itemList);
      

      【讨论】:

        猜你喜欢
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 2014-08-20
        • 1970-01-01
        • 2016-05-19
        • 1970-01-01
        • 2010-11-22
        • 1970-01-01
        相关资源
        最近更新 更多