Reputation: 23
i am working on a calander app. a listview which shows all calendar available. how can add a checkbox to it and also the calendar selected before should show checked.
i want a list view like this.
textview cb
Upvotes: 0
Views: 2412
Reputation: 2173
You can use checkedTextView for you ListView rows(using custom adapter) and specify android:choiceMode="multipleChoice" to your list view
Here is a sample from my code:
<CheckedTextView
android:id="@+id/member_name"
android:layout_width="match_parent"
android:layout_height="48dp"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:textColor="@color/dark_grey_txt" />
Here, it will add checked drawable as you tap on CheckedTextView
Now,Store selected items in List<> and you can add and remove on click
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.member_name);
if (ctv != null) {
if (ctv.isChecked()) {
ctv.setChecked(false);
listAdapter.removeSelectedMembers(position);
} else {
ctv.setChecked(true);
listAdapter.setSelectedMembers(position);
}
}
}
//adapter Methods of adding and removing Item
public void setSelectedMembers(int position) {
if (!selectedMembersList.contains(String.valueOf(position))) {
selectedMembersList.add(String.valueOf(position));
}
}
public void removeSelectedMembers(int position) {
selectedMembersList.remove(String.valueOf(position));
}
Upvotes: 0
Reputation: 2823
Set the listview adapter to "simple_list_item_multiple_choice"
ArrayAdapter<String> adapter;
List<String> values; // put values in this
//Put in listview
adapter = new ArrayAdapter<UserProfile>(
this,
android.R.layout.simple_list_item_multiple_choice,
values);
setListAdapter(adapter); //Set the adpter to list View
Second method would be to create a Custom Adapter By extending the Base adapter class:
Look at the example in the link:
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
Upvotes: 1