ListView + CheckBox =? [duplicate]

百般思念 提交于 2019-12-04 02:31:53

问题


Possible Duplicate:
Gmail-like ListView with checkboxes (and using the ActionBar)

I need :

ListView with

items { CheckBox, then TextView } ;

when you press on the CheckBox , item should change it's color.

How can I get it ?

P.S.

In other words I need a ListView with CheckBoxes like in Gmail app


回答1:


The answer is quite simple! There's a component called CheckedTextView which is a combination of a TextView and a CheckBox. This component might simplify your logic, leaving you free to do modify anything you want in your list's OnItemClickListener!

I've wrote an example for you:

    public class CheckBoxListView extends ListActivity implements OnItemClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Mock data
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };

        //android's simple_list_item_multiple_choice is a CheckedTextView
        //try creating your own later ;)
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, values);

        getListView().setOnItemClickListener(this);

        setListAdapter(adapter);

    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        CheckedTextView item = (CheckedTextView) arg1;

        //The change color logic is here!
        if(item.isChecked()) {
            item.setTextColor(Color.BLACK);
            item.setChecked(false);
        }
        else {
            item.setTextColor(Color.RED);
            item.setChecked(true);
        }

    }

}



回答2:


Create custom adapter and set listeners on checkboxes that will change what you want. (I'm not posting any code until I see what you've done on your own). If you don't know how Android docs are really good place to start. For this issue you should especially check LayoutInflater, BaseAdapter, ListActivity and creating xml resources (layouts to be exact).



来源:https://stackoverflow.com/questions/12821663/listview-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!