5

I need to create a custom alert dialog with single choice. But items have their own layout:

[VIEW(just color)_______TEXT_______RADIOBUTTON]

I have created a custom layout for listview, custom adapter and have a nice result enter image description here

But i cant make single choice, no one listener sets to listview...: Here is my adapter :

public class AlertListAdapter extends BaseAdapter {

ArrayList<AlertChoiceItem> mData;
Context mContext;
LayoutInflater inflater;
public AlertListAdapter(ArrayList<AlertChoiceItem> data, Context context) {
    mData = data;
    mContext = context;
    inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
    return mData.size();
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.item_alert_list, null);
    }
    TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitleAlertList);
    View v = (View) convertView.findViewById(R.id.vPriorAlertList);
    v.setBackgroundColor(GetColorByPriority.getColor(position, mContext));
    tvTitle.setText(mData.get(position).getTitle());
    RadioButton rb = (RadioButton) convertView.findViewById(R.id.rbRadioAlertList);


    return convertView;
}

}

Here i create alert:

AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                // dialog.setContentView(R.layout.alert_list_radio);
                dialog.setTitle("List Title");
                View customView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.alert_list_radio, null, false);
                ListView listView = (ListView) customView.findViewById(R.id.lvAlertList);

                // ArrayAdapter<String> ad = new ArrayAdapter<String>(this,
                // R.layout.single_item_layout , R.id.singleItem, dummies);
                ArrayList<AlertChoiceItem> itemList = new ArrayList<AlertChoiceItem>();
                itemList.add(new AlertChoiceItem(false, "Critical", 5));
                itemList.add(new AlertChoiceItem(false, "High", 4));
                itemList.add(new AlertChoiceItem(false, "Medium", 3));
                itemList.add(new AlertChoiceItem(false, "Low", 2));
                itemList.add(new AlertChoiceItem(false, "Very low", 1));
                itemList.add(new AlertChoiceItem(false, "Off filter", 0));

                AlertListAdapter mAdapter = new AlertListAdapter(itemList, getActivity());
                listView.setAdapter(mAdapter);
                listView.setOnItemClickListener(mOnItemClick);
                listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                dialog.setView(customView);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });
                dialog.show();

How to add single choice?

UPD: AlertChoiceItem implen:

public class AlertChoiceItem {

private boolean isChecked;
private String title;
private int prior;
public AlertChoiceItem(boolean isChecked, String title, int prior) {
    super();
    this.isChecked = isChecked;
    this.title = title;
    this.prior = prior;
}
public boolean isChecked() {
    return isChecked;
}
public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public int getPrior() {
    return prior;
}
public void setPrior(int prior) {
    this.prior = prior;
}

}

4
  • why you are doing that much thing ..the total task will finish when you use a spinner in listview if you want single choice.. Commented Feb 28, 2014 at 11:54
  • I cant use spinner. They wants listview :D Commented Feb 28, 2014 at 11:55
  • Because i have design :/ Commented Feb 28, 2014 at 11:57
  • stackoverflow.com/questions/8337180/… Commented Feb 28, 2014 at 12:03

4 Answers 4

2

Solve problem in such way:

1) Make all(!) views in layout not focusable.

2) Set onClickItemListener in activity to listview

3) With every click send to adapter signal to remove all checks, set new(by position) and re-draw listview.

Sign up to request clarification or add additional context in comments.

Comments

1

Set in item_alert_list xml file

android:focusable = "false"
android:focusableInTouchMode="false"

for your all UI elements.

Comments

0

You may try adding ItemClick listener to your listview

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // do whatever you want

        }
    });

1 Comment

It's not work... No effect. The same thing with "focusable = false" in radiobutton xml
0

Check your implementation of AlertChoiceItem. Make sure it implements the Checkable interface as well as passes it through to the underlying RadioButton you are using.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.