I am showing an alert dialog to choose the list. Now I want to show the default list as selected in the alert dialog. I have attached the custom adapter to the list. I have used setSingleChoiceItems in the dialog.
For this I have set the second argument to 0 but still I am unable to see the selected item.
code:
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
I tried as suggested, still dint get the selected item.
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;
}
}
adapter initialization
final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,allTables,0);
EDIT:
Removed .Create() method. It shows an error over alert dialog initialization.
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();
}
What's wrong here? Thank you..
At the end of getViewMethod in your adapter class add the following code:
viewHolder.rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
if(!isFirstTime) {
YouDialogHoldingClass.itemSelected(position);
}
else {
isFirstTime = false;
}
//selectedItem = position;
//notifyDataSetChanged();
}
}
});
if(position == selectedItem) {
isFirstTime = true;
viewHolder.rb.setChecked(true);
} else {
viewHolder.rb.setChecked(false);
}
return convertView;
Instead of setting singleChoiceItems() method set the following method:
alertDialog.setAdapter(adapter, null);
Make a static method in the class where you are showing the dialog:
public static void itemSelected(int tempPos) {
// do what you want on item selection....
}
Show the dialog as following:
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.Builder builder = 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,mAllLists,selectedItem);
builder.setAdapter(adapter, null);
alertDialog = builder.create();
alertDialog.show();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.