简体   繁体   English

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

[英]How to place Checkboxes with ListView (Android)

I was following the ListView official tutorial - [url]http://developer.android.com/resources/tutorials/views/hello-listview.html[/url], because I get the entries dinamically through a webservice.我正在关注 ListView 官方教程 - [url]http://developer.android.com/resources/tutorials/views/hello-listview.html[/url],因为我通过网络服务动态地获取条目。

Problem is, I want to place checkboxes at it.问题是,我想在上面放置复选框。 I have this我有这个

(aux (String [] aux) is previously and correctly fullfilled before this) (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);

And I think the problem is on my row.xml我认为问题出在我的行上。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" />

or in this function或在此 function

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;

What happens is: it appears the checkbox, but in front instead the right text it appears "android.widget.Checkbox@...."发生的情况是:它出现了复选框,但在前面显示的是正确的文本“android.widget.Checkbox@....”

What am I doing wrong?我究竟做错了什么?

You have to implement your own Adapter extending ArrayAdapter and put a TextView in your row.xml您必须实现自己的适配器扩展 ArrayAdapter 并在您的行中放置 TextView。xml

Then you must handle your view from getView() method.然后你必须从 getView() 方法处理你的视图。

Let me know if this helps, if not, please can you add more info about what you need/want a more code for sure.让我知道这是否有帮助,如果没有,请您添加更多关于您需要/想要更多代码的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM