带有builder.setSingleChoiceItems的AlertDialog.禁用项目

问题描述:

我正在使用以下方法来设置名为mMapGoogleMap对象的mapType.

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", "Satellite", "Hybrid"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Set map type");

    int checkItem = 0;

    builder.setSingleChoiceItems(
            MAP_TYPE_ITEMS,
            checkItem,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                        switch (item) {
                            case 0:
                                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                                break;
                            case 1:
                                mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                                break;
                            case 3:
                               mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                                break;
                        }

                    dialog.dismiss();
                }
            }
    );

    AlertDialog fMapTypeDialog = builder.create();
    fMapTypeDialog.show();

}

我想做的是禁用其中一个选择,比方说第一个(道路). 我该怎么办?

What I am trying to do is disable one of the choices, let's say the 1st one (Road). How could I do that?

PS1我阅读了此 AlertDialog单选列表-我需要一些不可点击的项目,但我不知道如何在我的情况下使其正常工作.

P.S.1 I read this AlertDialog with single choice list - I need some items nonclickable but I don't understand how could I make it work in my case.

PS2我也尝试过此解决方案:

P.S.2 I tried, this solution too: Android: AlertDialog - how to disable certain choices that are not available Nothing happens. All options are enabled.

可以在标准AlertDialog中执行此操作,但可以使用自定义列表适配器.您发布的第一个链接对您不起作用的原因也许是因为在填充对话框之前更新列表项很重要.

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.

创建对话框:

    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();

自定义适配器:

  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;
    }

}