我需要创建一个单一选择的自定义警报对话框。 但是项目有自己的布局:
[观看(只是颜色)_______文字_______ RADIOBUTTON]
我为listview,自定义适配器创建了一个自定义布局,并且结果很好
但我无法做出单一选择,没有一个听众设置为listview ...: 这是我的适配器:
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();
如何添加单项选择?
UPD: 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;
}
}
答案 0 :(得分:2)
以这种方式解决问题:
1)使布局中的所有(!)视图都不可聚焦。
2)将活动中的onClickItemListener设置为listview
3)每次点击发送到适配器信号以删除所有支票,设置新(按位置) 并重新绘制listview。
答案 1 :(得分:1)
在 item_alert_list xml文件中设置
android:focusable = "false"
android:focusableInTouchMode="false"
用于所有UI元素。
答案 2 :(得分:0)
您可以尝试将ItemClick侦听器添加到列表视图
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// do whatever you want
}
});
答案 3 :(得分:0)
检查AlertChoiceItem
的实施情况。确保它实现了Checkable
接口,并将其传递给您正在使用的基础RadioButton
。