Alertdialog With Builder.setsinglechoiceitems. Disable Items
I am using the following method do set the mapType of a GoogleMap object named mMap. private void setMapType() { final CharSequence[] MAP_TYPE_ITEMS = {'Road', 'Sat
Solution 1:
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.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle("Set map type");
intcheckItem=0;
ArrayList<Integer> list = newArrayList<Integer>();
//specify index 1 is disabled, 0 and 2 will be enabled as normal
list.add(Integer.valueOf(1));
finalMyCustomAdapteradapter=newMyCustomAdapter(MAP_TYPE_ITEMS, list);
builder.setAdapter(adapter, null );
builder.setSingleChoiceItems(
MAP_TYPE_ITEMS,
checkItem,
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int item) {
switch (item) {
case0:
Toast.makeText(InitialActivity.this, "Item 0 clicked ", Toast.LENGTH_SHORT).show();
break;
case1:
Toast.makeText(InitialActivity.this, "Item 1 clicked ", Toast.LENGTH_SHORT).show();
break;
case2:
Toast.makeText(InitialActivity.this, "Item 2 clicked ", Toast.LENGTH_SHORT).show();
break;
}
if( adapter.getItemViewType(item) == 0 ){
dialog.dismiss();
}
}
}
);
AlertDialogfMapTypeDialog= builder.create();
fMapTypeDialog.show();
The custom adapter:
privateclassMyCustomAdapterextendsBaseAdapter {
private ArrayList<String> mData = newArrayList<String>();
private ArrayList<Integer> mDisabled = newArrayList<Integer>();
private LayoutInflater mInflater;
publicMyCustomAdapter(CharSequence[] items,
ArrayList<Integer> disabledItems) {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDisabled = disabledItems;
for( inti=0; i< items.length; ++i ){
addItem( items[i].toString());
}
}
publicvoidaddItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@OverridepublicintgetItemViewType(int position) {
if( mDisabled.contains(position))
return1; //disabledreturn0; //enabled as normal
}
@OverridepublicintgetViewTypeCount() {
return2;
}
@OverridepublicintgetCount() {
return mData.size();
}
@Overridepublic String getItem(int position) {
return mData.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ViewHolderholder=null;
inttype= getItemViewType(position);
if (convertView == null) {
holder = newViewHolder();
switch(type) {
case1:
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;
case0:
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;
}
}
Solution 2:
Basically, you cann't do it, with simple AlertDialog and Builder. What you trying to do, it's exchange your Views during some interaction, but that items doesn't have such behavior.
But it isn't problem to do it with Custom Dialog. Just for Example...
// create a Dialog componentfinalDialogdialog=newDialog(context);
//Tell the Dialog to use the dialog.xml as it's layout description // With your own Layouts and CheckBoxs
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Android Custom Dialog Box");
TextViewheaderTextView= (TextView) dialog.findViewById(R.id.txt);
headerTextView .setText("This is an Android custom Dialog Box Example! Enjoy!");
ButtondialogButton1= (Button) dialog.findViewById(R.id.dialogButton1);
dialogButton1.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
dialogButton1.setEnabled(false);
dialog.dismiss();
}
});
// And so on, with other buttons
dialog.show();
Solution 3:
I answered a pretty similar question here. Basically you can set a listener when a view is added in a given ListView and disable it.
Post a Comment for "Alertdialog With Builder.setsinglechoiceitems. Disable Items"