【问题标题】:Calling notifyDataSetChanged() throws exception调用 notifyDataSetChanged() 抛出异常
【发布时间】:2010-10-07 15:53:19
【问题描述】:

我有一个绑定到适配器的 ListView。适配器是我的主类的受保护成员。我有一个侦听器线程,它接收数据并更新我的适配器的源列表。当我调用适配器的 notifyDataSetChanged() 方法时,抛出异常:

运行时异常:PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) 行:322

我读到必须在 UI 线程上调用 notifyDataSetChanged() 方法,我正在这样做。事实上,为了确定,我什至在 UI 线程上更新了 ListView 的数据源。我在侦听器线程中所做的只是更新包含数据的受保护成员。这是我正在做的一个示例(请注意,我没有调用 AsyncTask 的代码部分,但它是从侦听器中触发的):

public class main extends Activity() {

 protected LinkedList<HashMap<String, String>> adapterDataList = new LinkedList<HashMap<String, String>>();
 protected MyDataObject dataObject;
 protected SimpleAdapter dataAdapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {

     //call parent oncreate
     super.onCreate(savedInstanceState);

  //display the main form
  setContentView(R.layout.main);

  //get a handle on the score list view elements
  ListView dataList = (ListView)(findViewById(R.id.datalist));

  //link the adapter and the data source
  dataAdapter = new SimpleAdapter(this, adapterDataList, R.layout.row, new String[]{"name","address"}, new int[]{R.id.username,R.id.address});

  dataList.setAdapter(dataAdapter);

 }

 protected void refreshData() {
  adapterDataList.clear();
  Iterator<MyData> iter = MyDataObject.iterator();
  while(iter.hasNext()) {
   HashMap<String, String> item = new HashMap<String, String>()
   item.put("name", iter.name);
   item.put("address", iter.address);
   adapterDataList.add(item);
  }

  dataAdapter.notifyDataSetChanged();
 }

 private class DataTask extends AsyncTask<Data, Void, Data> {
  protected void onPostExecute(Data data) {
   dataObject = data;
   runOnUiThread (new Runnable() { public void run() {
    refreshScores();
   }});
  }
 }

 ...
}

是否有人对我为什么会收到此异常有任何指示或想法?非常感谢任何帮助。

编辑 以下是布局文件,根据要求: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ptl="http://schemas.android.com/apk/res/com.mod0.pubtrivialive"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/datalistcontainer">

    <ListView android:id="@+id/datalist" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingBottom="6dip"
  android:paddingTop="4dip">

  <TextView android:id="@+id/name"
  android:layout_width="80dip"
  android:layout_height="wrap_content" />

  <TextView android:id="@+id/address"
  android:layout_height="wrap_content"
  android:layout_weight="1" />

</LinearLayout>

【问题讨论】:

  • 您可以考虑发布您的行布局文件。
  • 谢谢,我添加了上面的布局。
  • 您需要发布更多代码。你使用了一堆你没有定义的变量adapterDataList.add(item);

标签: java android adapter


【解决方案1】:

这看起来不对dataList.clear();。你确定你是要在dataList 而不是adapterDataList 上调用clear 吗?

【讨论】:

  • 对不起,我又编辑了。我已经更改了变量名称以使事情更清晰,并且把那部分弄乱了。我将在 while 循环中创建的每个项目添加到 dataList,然后在适配器上调用 notifyDataSetChanged()。我在 while 循环之前调用 dataList.clear() 来删除旧数据,以便我可以用新数据填充它。
  • 你准备好使用SimpleAdapter了吗?我建议您制作自己的适配器类,扩展 BaseAdapterArrayAdapter
  • 我能做到;我只是希望保持事情,呃,简单。 :) 如果我可以使用自定义适配器,我会报告。
  • 通常,当您必须添加/删除视图、修改内容以及通常对列表进行处理时,您可能应该实现自己的内容。
  • 实施我自己的观点确实有所帮助。它迫使我进入那里并真正弄清楚事情是如何运作的。 FWIW,inflater 抛出异常的原因是地址 TextView 没有 width 属性,这是必需的。我只是通过在适配器 getView 方法中执行 try/catch 并记录异常才发现这一点,这只是因为我创建了自己的适配器才可能。感谢您的提示!
猜你喜欢
  • 2013-05-24
  • 2020-03-10
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
相关资源
最近更新 更多