Reputation: 4129
I would like to implement a Custom Multiple Choice Dialog
, so following the directions from this answer, this is what I did:
I created the xml
for my row, which is a CheckedTextView
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-condensed"
style="?android:textAppearanceSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"/>
Now, my Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.dialog_list_item, tables), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alerDialog.show();
As you can see, I have not implemented an setOnItemClickListener
yet, but I do not know how though. However, the Dialog
looks fine. How can I call a ClickListener for each of the CheckedTextView
?
Than you very much
Upvotes: 0
Views: 4596
Reputation: 4129
The way that @Rahul Gupta suggested works after implementing a ListView
and OnItemClickListener
, but if the ListView
contains too many items, the Views
that are not shown are going to be "checked" as well. I suppose the Views
are not yet generated until the user actually scrolls down and see the rest of the items.
The way that I made it work was creating a Layout
with a single ListView
and setting the value of choice mode
to CHOICE_MODE_MULTIPLE
. In that case, I don't have to deal with each item, but I can retrieve an array of the items selected by using listview.getCheckedItemPositions()
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View view= li.inflate(R.layout.listview_dialog, null);
builder.setView(view);
builder.setTitle(title);
listView = (ListView) view.findViewById(R.id.listview);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), R.layout.dialog_list_item , R.id.text2, tables);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setDivider(null);
builder.create();
Upvotes: 3
Reputation: 5295
Yes, you can implement thee onClickListener for your CheckedTextView
.
You just have to find its reference. Now as it is in your dialog, you will have to do this :-
CheckedTextView dialogCheckedTextView = (CheckedTextView ) alertDialog.findViewById(R.id.yourcheckedtextboxid);
dialogCheckedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO
}
});
Upvotes: 0