0

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

Community
  • 1
  • 1
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69

2 Answers2

3

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();
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
0

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
    }
 });
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
  • `NullPointerException` at `dialogCheckedTextView.setOnClickListener....` Any ideas? – Luis Lavieri Feb 23 '14 at 17:05
  • This is how you do it only. Check that you passeed the correct reference – Rahul Gupta Feb 23 '14 at 18:02
  • I made it work implementing it inside `listview.setOnItemClickListener...`. However, it gave a different problem at the end and I had to change the implementation. At the end I found a way that works better for my case. Thanks though. – Luis Lavieri Feb 25 '14 at 01:54