如何使用ListView(Android)放置复选框

时间:2011-07-16 12:17:01

标签: java android

我正在关注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 @ ....”

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您必须实现自己的适配器扩展ArrayAdapter并在Text.xml中放置TextView

然后你必须从getView()方法处理你的视图。

如果有帮助,请告诉我,如果没有,请您添加更多关于您需要/想要更多代码的信息。

相关问题