开发者

How to place Checkboxes with ListView (Android)

开发者 https://www.devze.com 2023-03-21 04:44 出处:网络
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.

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.

Problem is, I want to place checkboxes at it. I have this

(aux (String [] aux) i开发者_如何学编程s previously and correctly fullfilled before this)

//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

<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

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@...."

What am I doing wrong ?


You have to implement your own Adapter extending ArrayAdapter and put a TextView in your row.xml

Then you must handle your view from getView() method.

Let me know if this helps, if not, please can you add more info about what you need/want a more code for sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号