Reputation: 6728
I want to display a multichoice list in an alert dialog box. If I'm using an array to store the list of items, then it is working fine :
d.setMultiChoiceItems(R.array.items,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do some stuff */
}
})
But in my case, the item list is dynamic, which I'm geting from a database. The database keeps on updating its contents and hence the list is also updating at a fixed interval of time. So, instead of using an array I would like to use a cursor in the argument of setMultiChoiceItems. Can anyone please tell me how to do it....?
Upvotes: 0
Views: 2978
Reputation: 1
we in JohnBryce Jerusalem, course id 5124 solved this problem by implementing it ourselves as follows:
final Map<Long,Boolean> selectedContactsMap = new HashMap<Long,Boolean>();
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Share link");
View selectContactView = getLayoutInflater().inflate(R.layout.contact_select_layout, null);
builder.setView(selectContactView);
ListView contactsListView = (ListView)selectContactView.findViewById(R.id.selectContactListView);
CursorAdapter adapter = new CursorAdapter(this, cursor, true)
{
@Override
public void bindView(View view, Context arg1, Cursor cursor)
{
final long id = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
final String displayName = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
TextView displayNameTextView = (TextView)view.findViewById(R.id.displayNameTextView);
final CheckBox selectContactCheckBox = (CheckBox)view.findViewById(R.id.selectContactCheckBox);
displayNameTextView.setText(displayName);
boolean isChecked = selectedContactsMap.containsKey(id)?
selectedContactsMap.get(id):false;
selectContactCheckBox.setChecked(isChecked);
selectContactCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
boolean isChecked = selectContactCheckBox.isChecked();
if(!isChecked && selectedContactsMap.containsKey(id))
{
Log.d(TAG, "Remove id: "+id+": "+displayName);
selectedContactsMap.remove(id);
}
else
{
Log.d(TAG, "Select id: "+id+": "+displayName+" : "+isChecked);
selectedContactsMap.put(id, isChecked);
}
}
});
}
@Override
public View newView(Context context, Cursor arg1, ViewGroup arg2)
{
View selectContactItemView = getLayoutInflater().inflate(R.layout.contact_select_item_layout, null);
return selectContactItemView;
}
};
contactsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
WebViewerFragment webViewerFrag = (WebViewerFragment)
getFragmentManager().findFragmentById(R.id.web_viewer_fragment);
String currentLink = null;
if (webViewerFrag != null)
{
currentLink = webViewerFrag.getCurrentLink();
long _id = (Long)view.getTag();
share(_id, currentLink);
}
else
{
Toast.makeText(MainActivity.this,"No link is found", Toast.LENGTH_LONG).show();
}
shareDialog.dismiss();
}
});
contactsListView.setAdapter(adapter);
builder.setView(selectContactView);
builder.setPositiveButton("Share!", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,":-(",Toast.LENGTH_SHORT).show();
}
});
shareDialog = builder.create();
}
Upvotes: 0
Reputation: 1007474
Just use another version of setMultiChoiceItems()
, the one that takes a Cursor
as a parameter. You also give it the name of the columns in the result set that represent the label and the boolean checkbox setting.
Upvotes: 3