Question

i'm trying to implement a custom multichoice dialog. My goal is to have a list with flags and language names. In the dialog i want to be able to select multiple rows. So far i done this:

checkedItems = new boolean[languages.size()];
for (int i=0; i<languages.size(); i++) {
    languages.get(i).selected = (selectedLangIndexes.contains(i));
    checkedItems[i] = (selectedLangIndexes.contains(i));
}

final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Select Languages");
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        selectedLangIndexes.clear();
        for (int i=0; i<checkedItems.length; i++) {
            languages.get(i).selected = false;
            if (checkedItems[i]) {
                selectedLangIndexes.add(i);
                languages.get(i).selected = true;
            }
        }
        // display my selected languages
        dialog.dismiss();
    }
});

LanguageAdapter adapter = new LanguageAdapter(this, R.layout.language_row,  languages.toArray(new Language[languages.size()]));
DialogInterface.OnClickListener langClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        checkedItems[which] = !checkedItems[which];
        // ? update the clicked row here ?              
    }
};

builder.setSingleChoiceItems(adapter, -1, langClickListener);

AlertDialog alertDialog = builder.create();
alertDialog.show();

Inside my LangAdapter's getView() method i'm setting the color of the row according to the selected property:

if (items[position].selected) {
    holder.txtTitle.setTextColor(context.getResources().getColor(R.color.lang_selected));
} else {
    holder.txtTitle.setTextColor(context.getResources().getColor(R.color.lang_not_selected));
}

The idea is to make the selected languages display with one color, and the other ones with another color. This works for me when the dialog gets created/reopened after i click the save button.

But how can i update the currently clicked item (change the textcolor) before dismissing the dialog?

UPDATE: adapter.notifyDataSetChanged(); doesn't work either.

Was it helpful?

Solution 2

After some time and research i finally got it.

After creating the dialog, i set a OnItemClickListener to the listView:

alertDialog = builder.create();
alertDialog.show();

alertDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
        checkedItems[pos] = !checkedItems[pos];
        TextView tv = (TextView) view.findViewById(R.id.tvLanguageName);
        if (checkedItems[pos]) {
            tv.setTextColor(ThisClass.this.getResources().getColor(R.color.lang_selected));
        } else {
            tv.setTextColor(SubtitleSearcher.this.getResources().getColor(R.color.lang_not_selected));
        }
    }

}); 

the above method:

DialogInterface.OnClickListener langClickListener(...)

can be deleted.

OTHER TIPS

i don't know how to do dynamically update of list in dialog.
so in my case i dismiss and again open dialog. (i received tariff for concrete phoe)
what do you think?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top