0

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

Hafiz
  • 23
  • 1
  • 9

2 Answers2

1

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

Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
  • simple_list_item_multiple_choice this thing is fine. but how can i work with checked unchecked state??? – Hafiz Mar 02 '13 at 08:02
  • please have a look at the following links it would clear all your doubts: http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/ http://wptrafficanalyzer.in/blog/listview-with-checkboxes-in-android/ – Karan_Rana Mar 02 '13 at 08:08
0

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));
}
Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26