I created an AlertDialog
that has a ListView
of items along with check boxes. I want to be able to collect the checked items in the calling activity so that I can use them.
I have a button in my activity:
<Button android:id="@+id/my_button" />
That I get like so:
Button button = (Button)findViewById(R.id.my_button);
Then I set a listener:
button.setOnClickListener(my_on_click_listener);
View.OnClickListener my_on_click_listener = new View.OnClickListener(){
@Override
public void onClick(View v){
my_method();
}
};
That calls a method:
public void my_method(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_listview_layout, null);
dialogBuilder.setView(view);
final DatabaseHelper databaseHelper = new DatabaseHelper(this);
String[] fromColumns = {"_id","value"};
int[] toViews = {R.id.dropdown_id, R.id.dropdown_value};
final ListView listView = (ListView)view.findViewById(R.id.my_listview);
final Cursor cursor = databaseHelper.getData();
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.my_layout, cursor, fromColumns, toViews, 0);
listView.setAdapter(simpleCursorAdapter);
// I tried this, but I'm not sure what to use for the isCheckedColumn and labelColumn
dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked){
// do stuff here ...
}
});
dialogBuilder.setPositiveButton("Save Selected", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
// I tried this, but it only collects the elements within view
for(int i=0; i<listView.getCount(); i++){
LinearLayout ll = (LinearLayout)listView.getChildAt($i);
if(null != ll){
CheckBox cb = (CheckBox)ll.findViewById(R.id.dropdown_checkbox);
TextView tv01 = (CheckBox)ll.findViewById(R.id.dropdown_id);
TextView tv02 = (CheckBox)ll.findViewById(R.id.dropdown_value);
}
}
}
});
final AlertDialog alert = dialogBuilder.create();
alert.setTitle("Select Choices");
alert.show();
}
Here is my_listview_layout.xml:
<ListView android:id="@+id/my_listview" />
Here is my_layout.xml:
<LinearLayout>
<CheckBox android:id="@+id/dropdown_checkbox" />
<TextView android:id="@+id/dropdown_value" />
<TextView android:id="@+id/dropdown_id" android:visibility="invidible"/>
</LinearLayout>
See my two comments in the code above.
So, how can I get my checked values from a ListView populated by a cursor within an AlertDialog in Android?
----
UPDATE:
Within my code, I commented out this line:
listView.setAdapter(simpleCursorAdapter);
and changed this line:
dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener();
to this:
dialogBuilder.setMultiChoiceItems(cursor, "_id", "value", new DialogBuilder.OnMultiChoiceClickListener()
and now my dialog is rendering properly, and I can determine which items are checked. I guess [maybe] both the ListView
and setMultiChoiceItems()
were fighting for control over drawing the list of choices.
However, I have a new issue. if I check some items at the top of the list, then scroll down and back up, the items I checked have been unchecked. That, and the first item in the list is checked by default and I cannot figure out how to uncheck it.
Any ideas???
Thanks ... again.