Android ListView不会更新适配器的更改

时间:2012-08-24 23:36:30

标签: android listview android-arrayadapter

我在ListView中的ArrayAdapter中更新数据时遇到问题,甚至在listAdapter.notifyDataSetChanged()上也没有反映在ListView中。

有趣的是初始加载很好,我得到了ListView中呈现的完整数据列表。这是当我第二次加载活动时,缓存的ArrayList传递给setDataList()方法不会被渲染。生成的ListView仅包含onCreate()方法中的单个默认条目设置。

我调试了代码并逐步完成了runnable,它似乎按照我的预期运行,只是没有最终结果。

如果有人能帮忙解决这个问题,我会非常感激,我已经用Google搜索了它!

我的代码片段:

public class CriteriaOriginIncludeActivity extends Activity {   

private CriteriaOriginIncludeController m_controller;
private CriteriaOriginIncludesAdapter m_listAdapter;
private ArrayList<OriginRowData> m_listData;
private ListView m_originList;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.criteria_origin_includes);      
    ArrayList<OriginRowData> list = new ArrayList<OriginRowData>();
    list.add(new OriginRowData(new Origin(0,""), false));

    m_listAdapter = new CriteriaOriginIncludesAdapter(this,
                                                                R.layout.origin_include_list_row,
                                                                R.id.lst_origin_include,
                                                                list);

    m_originList = (ListView) this.findViewById(R.id.lst_origin_include);
    m_originList.setAdapter(m_listAdapter);

    m_controller = new CriteriaOriginIncludeController(this);

}

public void setOriginList(ArrayList<OriginRowData> list) {

    if (list != null && list.size() > 0) {
        m_listData = list;
    } else {
        m_listData = new ArrayList<OriginRowData>();
    }

    runOnUiThread(returnRes);
}

private Runnable returnRes = new Runnable() {

public void run() {

    m_listAdapter.clear();
    if(m_listData != null && m_listData.size() > 0){
        m_listAdapter.notifyDataSetChanged();
        for(int i=0;i<m_listData.size();i++)
            m_listAdapter.add(m_listData.get(i));
    }
    m_listAdapter.notifyDataSetChanged();
}
};
}

适配器:

public class CriteriaOriginIncludesAdapter extends ArrayAdapter<OriginRowData> {
private ArrayList<OriginRowData> m_items;
    private Context m_context;

    public CriteriaOriginIncludesAdapter(Context context, int resourceId, int           textViewResourceId, ArrayList<OriginRowData> list) {
      super(context, resourceId, textViewResourceId, list);
      this.m_context = context;
      m_items = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      if (v == null) {
        LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.origin_include_list_row, null);
      }
      OriginRowData o = m_items.get(position);
      if (o != null) {
        TextView txtName = (TextView) v.findViewById(R.id.txt_origin_include_name);
        TextView txtId = (TextView) v.findViewById(R.id.txt_origin_include_id);
        CheckBox chkOrigin = (CheckBox) v.findViewById(R.id.chk_origin);

        if (txtName != null) {
          txtName.setText(o.getOrigin().getOrigin());                            
        }
        if (txtId != null) {
          txtId.setText(Integer.toString(o.getOrigin().getId()));                            
        }
        if (chkOrigin != null) {
          chkOrigin.setChecked(o.getInclude());

          chkOrigin.setOnClickListener(new OriginIncludeOnClickListener(position));
        }

      }

      return v;
   }

}

1 个答案:

答案 0 :(得分:0)

您需要覆盖ArrayAdapter的{​​{1}}方法。

由于您使用自己的私有Add(),因此默认private ArrayList<OriginRowData> m_items;方法不知道必须将数据添加到此数组列表中。

在你的getView中你正在使用Add()数组,所以这需要更新。默认的m_items方法会将项目添加到ArrayAdapter的内置