Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

like image 329
onCreate Avatar asked Oct 21 '22 13:10

onCreate


2 Answers

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.

like image 130
onCreate Avatar answered Oct 23 '22 09:10

onCreate


Set in item_alert_list xml file

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

for your all UI elements.

like image 21
Piyush Avatar answered Oct 23 '22 11:10

Piyush