onCreate
onCreate

Reputation: 775

Custom alert dialog with custom listview

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;
}

}

Upvotes: 5

Views: 9658

Answers (4)

Piyush
Piyush

Reputation: 18923

Set in item_alert_list xml file

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

for your all UI elements.

Upvotes: 1

onCreate
onCreate

Reputation: 775

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.

Upvotes: 2

Larry Schiefer
Larry Schiefer

Reputation: 15775

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.

Upvotes: 0

meborda
meborda

Reputation: 391

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

        }
    });

Upvotes: 0

Related Questions