AlertDialog列表中的自定义对象;如何获取显示字符串,然后获取实际值? [英] Custom objects in AlertDialog list; how to get a display string and then the actual value?

查看:93
本文介绍了AlertDialog列表中的自定义对象;如何获取显示字符串,然后获取实际值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看Android AlertDialog,它很容易使用setItems(...)添加要显示的字符串列表.

I have been looking at Android AlertDialog, and its easy enough to use the setItems(...) to add a list of Strings that are to be shown.

但是,在大多数情况下,您想要一个显示漂亮的字符串的列表,但是从列表中选择某些内容时,您需要的是实际值而不是字符串.

However, in most cases you want a list showing nice Strings, but when selecting something from the list you want the actual value and not the String.

我一直无法找到如何轻松便捷地做到这一点.

I have been unable to find how to do that in an easy and nice way.

提示? =)

final Button Button1 = (Button) findViewById(R.id.Button1);
Button1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        final CharSequence[] items = { "String 1", "String 2", "String 3" };
        // INstead of a string array, I want something like:
        // ArrayList<CustomObject> test = new ArrayList<CustomObject>(myArray);
        // And the CustomObject has a toString() and also a value. This array should in the best of worlds be the base for the list below =)

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle(LanguageHandler.GetString("Test"));
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                // ***   I want to get the value here!   ***

                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
});

推荐答案

您可以在警报对话框中使用Custom Adapter代替CharSequence[] items = { "String 1", "String 2", "String 3" };

Instead of CharSequence[] items = { "String 1", "String 2", "String 3" }; you can use a Custom Adapter in your Alert Dialog,

类似

AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this);
            builder.setTitle("Select");
            builder.setAdapter(adapter,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int item) {
                            Toast.makeText(MyApp.this, "You selected: " + items[item],Toast.LENGTH_LONG).show();
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

您的 list_row.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="48px"
        android:layout_height="48px"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/title"
        android:textColor="#0000FF"
        android:text=""
        android:paddingLeft="10dip"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

和您的 ListAdapter 类似,

String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" };

// Instead of String[] items, Here you can also use ArrayList for your custom object..

ListAdapter adapter = new ArrayAdapter<String>(
        getApplicationContext(), R.layout.list_row, items) {

    ViewHolder holder;
    Drawable icon;

    class ViewHolder {
        ImageView icon;
        TextView title;
    }

    public View getView(int position, View convertView,
            ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                .getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.list_row, null);

            holder = new ViewHolder();
            holder.icon = (ImageView) convertView
                    .findViewById(R.id.icon);
            holder.title = (TextView) convertView
                    .findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            // view already defined, retrieve view holder
            holder = (ViewHolder) convertView.getTag();
        }       

        Drawable drawable = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder

        holder.title.setText(items[position]);
        holder.icon.setImageDrawable(drawable);

        return convertView;
    }
};

这篇关于AlertDialog列表中的自定义对象;如何获取显示字符串,然后获取实际值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆