我正在展示一个弹出对话框来选择列表。现在我想在弹出对话框中显示默认的列表项作为选中项。我已经将自定义适配器附加到列表上了。我在对话框中使用了setSingleChoiceItems。
为此,我将第二个参数设置为0,但是我仍然无法看到选中的项。
代码:
ArrayList<ListData> allTables = new ArrayList<>();
allTables = mListTableHelper.getAllList();
final ListData taskList = new ListData();
taskList.setId(100000000);
taskList.setTitle("Default List");
allTables.add(0, taskList);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddTaskActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);
ListView lv = (ListView) convertView.findViewById(R.id.tableslist);
final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,allTables);
alertDialog.setSingleChoiceItems(adapter, 0 , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
ListData listOject = new ListData();
listOject = listData.get(which);
mListId = listOject.getId();
// listOject = mListTableHelper.getList(mListId);
String title = listOject.getTitle();
list.setText(listOject.getTitle());
dialog.dismiss();
}
}).create();
alertDialog.show();
ListItemAdapter
我按照建议尝试了一下,但仍然没有得到所选项目。
public class ListItemAdapter extends ArrayAdapter{
int selectedItem;
private static class ViewHolder {
TextView title;
RadioButton rb;
}
public ListItemAdapter(Context context, ArrayList<ListData> users,int selectedItem) {
super(context, R.layout.list_item, users);
this.selectedItem = selectedItem;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ListData item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item);
viewHolder.rb = (RadioButton) convertView.findViewById(R.id.radioButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.title.setText(item.getTitle());
// Return the completed view to render on screen
return convertView;
}
}
适配器初始化
final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,allTables,0);
编辑:
移除了.Create()方法。它在警告对话框初始化时显示错误。
public void showCustomList() {
mAllLists = new ArrayList<>();
mAllLists = mListTableHelper.getAllList();
final ListData taskList = new ListData();
taskList.setId(100000000);
taskList.setTitle("Default List");
mAllLists.add(0, taskList);
alertDialog = new AlertDialog(AddTaskActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);
ListView lv = (ListView) convertView.findViewById(R.id.tableslist);
final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,mAllLists,selectedItem);
alertDialog.setAdapter(adapter,null);
alertDialog.show();
}
这里出了什么问题?谢谢。