【发布时间】:2011-06-06 01:02:51
【问题描述】:
我正在尝试以完全编程的方式(不使用任何 xml)创建带有自定义列表项单元格的列表。我遇到了以下错误。在网上搜索了几个小时后,我将这篇文章作为我在 Stack Overflow 上的第一个问题。
感谢您对此的意见以及关于如何在不使用 R.*.xml 的情况下更好地处理 Android 编程的任何建议
在运行此代码时,我收到以下错误:
(暂停(异常 Resources$NotFoundException))
ListView.layoutChildren() 行:1596
ListView(AbsListView).onLayout(boolean, int, int, int, int) 行:1112
ListView(View).layout(int, int, int, int) 行:6569
FrameLayout.onLayout(boolean, int, int, int, int) 行:333 ...
代码:
公共类 MyList 扩展 ListActivity{
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//自定义列表
LinearLayout mainLayout=new LinearLayout(this);
ListView myListView=new ListView(this);
mainLayout.addView(myListView);
LinearLayout ListItemLayout=new LinearLayout(this);
TextView title = new TextView(this);
TextView author = new TextView(this);
TextView price = new TextView(this);
ListItemLayout.addView(title);
ListItemLayout.addView(author);
ListItemLayout.addView(price);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
ListItemLayout.getId(),
new String[] {"title","author","price"},
new int[] {ListItemLayout.getChildAt(0).getId(),ListItemLayout.getChildAt(1).getId(),ListItemLayout.getChildAt(2).getId()}
);
populateList();
setListAdapter(adapter);
}
private void populateList() {
HashMap<String,String> map = new HashMap<String,String>();
map.put("title",
"Professional Android ");
map.put("author", "Meier");
map.put("price", "$54.99");
list.add(map);
map = new HashMap();
map.put("title","Android 2");
map.put("author",
"Sayed");
map.put("price", "$48.98");
list.add(map);
}
}
【问题讨论】: