0

In my app, I have a ListView it contains text and check box. Suppose I selected some text views using checkbox. Then next time open this list I want to show the previous selected items. is it possible to check selected items based on the text in list view. It means first you are selected t1,t2 then next time list opens based on t1, t2 i want to check these texts. if yes then how to do it. Else suggest the better way to implement this.

List

enter image description here

Goofy
  • 6,098
  • 17
  • 90
  • 156
naresh
  • 10,332
  • 25
  • 81
  • 124
  • See this answer , it may help you, [http://stackoverflow.com/questions/8320663/android-how-to-redraw-list-when-the-user-sets-checkbox][1] [1]: http://stackoverflow.com/questions/8320663/android-how-to-redraw-list-when-the-user-sets-checkbox – Hasmukh Mar 22 '12 at 06:39

4 Answers4

0

Saved selected checkboxes index into preference and checked the checkbox into onCreate() or onResume() stub methods.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
MrWaqasAhmed
  • 1,479
  • 12
  • 12
0

suppose that you have a array store selected of all selected rows and named selectedArray

This code for storing it.

int count = selectedArray.lenght;

SharedPreferences prefs = getSharedPreferences("your_custom_name", Activity.MODE_PRIVATE);                prefs.edit().putInt("selected_count", count).commit();

for (int i = 0; i<count; i++){
    prefs.edit().putInt("selected_at_" + i, selectedArray[i]).commit();
}

This is for getting.

SharedPreferences prefs = getSharedPreferences("your_custom_name", Activity.MODE_PRIVATE);
int count = prefs.edit().getInt("selected_count", 0).commit();
for (int i = 0; i<count; i++){
    prefs.edit().getInt("selected_at_" + i, -1).commit();
}

Hope this help.

PhatHV
  • 8,010
  • 6
  • 31
  • 40
0

Step 1 : In your custom row use CheckedTextView instead of TextView and CheckBox

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/chkTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:textSize="14sp"
    android:textStyle="bold" />

Step 2: In your activity

public void onItemClick(AdapterView<?> arg0, View view, int postion, long id) {
    hold the selected item position in a array list (depends upon the item is checked or unchecked.)

}

Step 3: On Clicking of Submit Button persist the selected position in any of the storage technique (most preferable object cache).

Step 4 : While creating the Activity :
    Step 4.1 : Add the below Line

listvw = (ListView)findViewById(android.R.id.list);
listvw.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    Step 4.2 : Get the persisted ids If it very first time call then the persisted value is null or list size 0.

long[] checkedItemIds = retriveFromPersistence();
if (checkedItemIds != null) {
 for(int id_count = 0; id_count < checkedItemIds.length; id_count++) {
        listvw.setItemChecked((int) checkedItemIds[id_count], true);
    }
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90