开发者

Listview with checkboxes which saves staes

开发者 https://www.devze.com 2023-03-20 01:39 出处:网络
My application consists of 2 activities.The first activity contains a TextView,if you click on it, you move to the second activity that consists of a ListView and a button Done. The ListView contains

My application consists of 2 activities.The first activity contains a TextView,if you click on it, you move to the second activity that consists of a ListView and a button Done. The ListView contains TextViews with a CheckBox. If you click on button then it finish activity and returns selected text item. If you go back to the list view the selected checkboxe开发者_StackOverflows restored.

Code would be appreciated.


First of all I suggest you to use CheckedTextView control instead of a CheckBox & TextView. CheckedTextView serves as the combination of checkbox and textview and is easy to handle and implement.

Secondly, you should have an ArrayList of boolean of the exact size as the ListView no. of Items. Then you can set the ArrayList items accordingly in the OnListItemClick function of ListView. At any time and anywahere in your code, you can get the reference of your selection of the ListView. Its simple and efficient.

Here is a sample OnListItemClick code:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {       
        arrCheckBox.set(position, !arrCheckBox.get(position));
        CheckedTextView ctvListItem = (CheckedTextView)v.findViewById(R.id.ctvCustomLVRowID);
        ctvListItem.setChecked(arrCheckBox.get(position));
    }  

Here arrCheckBox is a boolean ArrayList which is keeping record of our selection and size of this array is same as no. of ListItems. I hope now you can figure it out.


XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
        android:layout_width="fill_parent"></ListView>
</LinearLayout>

Java code:

public class ListViewMultipleChoiceExample extends Activity {
    private ListView lView;
    private String lv_items[] = { "Android", "iPhone", "BlackBerry",
            "AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
            "Us", "UK", "India" };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        lView = (ListView) findViewById(R.id.ListView01);
        // Set option as Multiple Choice. So that user can able to select more
        // the one option from list
        lView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, lv_items));
        lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}

Listview with checkboxes which saves staes

0

精彩评论

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