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

标签 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@....”

我做错了什么?

最佳答案

您必须实现自己的适配器,扩展 ArrayAdapter 并将 TextView 放入 row.xml

然后您必须通过 getView() 方法处理您的 View 。

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

关于java - 如何使用 ListView 放置复选框(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6717294/

相关文章:

java - PrimeFaces 自定义数据表中的 RowEditor

java - 在单元测试中使用 Android SDK 类

java - 在加载 jtables 数据之前显示 jinternalframe

android - ListView 中的 ArrayAdapter getView() : what I see, 与链接到适配器的数据下的内容不匹配

java - 在Android中使用addView输出文本结果?

java - Spring Security ACL(访问控制列表)如何处理每个对象标识具有多个权限的主体?

java - Spring MVC 中的 jackson

android - 在LinearLayout中保持textview的高度

android - 干净地退出android应用程序

Android:自定义可选择的 ListView 项目