自定义带有自定义列表视图的警告对话框

5

我需要创建一个具有单选项的自定义警报对话框,但是这些选项都有自己的布局:

[VIEW(仅颜色)_______TEXT_______RADIOBUTTON]

我已经为列表视图创建了自定义布局和自定义适配器,并获得了很好的结果 enter image description here

但是我不能进行单选,没有监听器设置到列表视图中...: 这是我的适配器:

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

我将创建一个警报:

这里我创建警报:

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();

如何添加单选项?
更新: AlertChoiceItem 实现:
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;
}

}


为什么你要做那么多的事情呢?如果你想要在ListView中使用单选功能,只需要使用一个Spinner,整个任务就会完成了。 - Ranjit
我不能使用Spinner。他们想要ListView :D - onCreate
因为我有设计 :/ - onCreate
https://dev59.com/cWsy5IYBdhLWcg3wxAuO - Entreco
4个回答

2
以以下方式解决问题:
1)使布局中的所有视图都不可聚焦。
2)在活动中将onClickItemListener设置为listview。
3)每次单击时向适配器发送信号以删除所有检查,设置新的(按位置)并重新绘制listview。

1

item_alert_list xml 文件中设置

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

适用于所有用户界面元素。


0
你可以尝试为你的列表视图添加ItemClick监听器。
listView.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });

没有效果。 在RadioButton的XML中使用“focusable = false”也是同样的情况。 - onCreate

0
请检查您对AlertChoiceItem的实现。确保它不仅实现了Checkable接口,还将其传递给您使用的底层RadioButton组件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接