我正在关注ListView官方教程 - [url]http://developer.android.com/resources/tutorials/views/hello-listview.html[/url],因为我通过网络服务动态获取条目。
问题是,我想在它上面放置复选框。我有这个
(aux (String [] aux) 在此之前已正确填充)
//setListAdapter(new ArrayAdapter<String>(this, R.layout.lista, aux));
setListAdapter(new ArrayAdapter<CheckBox>(this,R.layout.row, convertToListItem(aux)));
//ListView lv = getListView();
//lv.setTextFilterEnabled(true);
我认为问题出在我的 row.xml
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="14sp" android:typeface="normal" android:textStyle="normal"
android:layout_weight="0" android:layout_gravity="left" />
或者在这个函数中
private List<CheckBox> convertToListItem(String[] aux2){
List<CheckBox> result = new ArrayList<CheckBox>();
ArrayList<CheckBox> boxes = new ArrayList<CheckBox>();
for (String text : aux2){
CheckBox temp = new CheckBox(this);
temp.append(text);
temp.setText(text,BufferType.NORMAL);
result.add(temp);
boxes.add(temp);
}
return result;
发生的情况是:出现了复选框,但前面出现了正确的文本“android.widget.Checkbox@....”
我做错了什么?
最佳答案
您必须实现自己的适配器,扩展 ArrayAdapter 并将 TextView 放入 row.xml
然后您必须通过 getView() 方法处理您的 View 。
请告诉我这是否有帮助,如果没有,请您添加更多有关您需要/想要更多代码的信息。
关于java - 如何使用 ListView 放置复选框(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6717294/