我在更新 ListView 中的 ArrayAdapter 中的数据时遇到问题,甚至在 listAdapter.notifyDataSetChanged() 上,更改也没有反映在 ListView 中。
有趣的是初始加载很好,我得到了在 ListView 中呈现的完整数据列表。这是当我第二次加载活动时,缓存的 ArrayList 被传递给 setDataList() 方法没有被渲染。生成的 ListView 仅包含 onCreate() 方法中的单个默认条目设置。
我已经调试了代码并逐步完成了可运行文件,它似乎按我的预期运行,只是没有最终结果。
如果有人可以提供帮助,我将不胜感激,我已经用谷歌搜索了它!
我的代码片段:
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;
}
}