It is possible to do this in a standard AlertDialog, but using a custom list adapter. Perhaps the reason the first link you posted did not work for you is because it is important that the list items are updated prior to the dialog being populated.
Creating your dialog:
final CharSequence[] MAP_TYPE_ITEMS =
{"Road", "Satellite", "Hybrid"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Set map type");
int checkItem = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
//specify index 1 is disabled, 0 and 2 will be enabled as normal
list.add(Integer.valueOf(1));
final MyCustomAdapter adapter = new MyCustomAdapter(MAP_TYPE_ITEMS, list);
builder.setAdapter(adapter, null );
builder.setSingleChoiceItems(
MAP_TYPE_ITEMS,
checkItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0:
Toast.makeText(InitialActivity.this, "Item 0 clicked ", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(InitialActivity.this, "Item 1 clicked ", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(InitialActivity.this, "Item 2 clicked ", Toast.LENGTH_SHORT).show();
break;
}
if( adapter.getItemViewType(item) == 0 ){
dialog.dismiss();
}
}
}
);
AlertDialog fMapTypeDialog = builder.create();
fMapTypeDialog.show();
The custom adapter:
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private ArrayList<Integer> mDisabled = new ArrayList<Integer>();
private LayoutInflater mInflater;
public MyCustomAdapter(CharSequence[] items,
ArrayList<Integer> disabledItems) {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDisabled = disabledItems;
for( int i = 0; i< items.length; ++i ){
addItem( items[i].toString());
}
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if( mDisabled.contains(position))
return 1; //disabled
return 0; //enabled as normal
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch(type) {
case 1:
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
convertView.setEnabled(false);
convertView.setClickable(false);
break;
case 0:
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
holder.textView = (TextView)convertView.findViewById(android.R.id.text1);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…