Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 对话框列表中的自定义对象;如何获取显示字符串和实际值?_Android_Android Alertdialog - Fatal编程技术网

Android 对话框列表中的自定义对象;如何获取显示字符串和实际值?

Android 对话框列表中的自定义对象;如何获取显示字符串和实际值?,android,android-alertdialog,Android,Android Alertdialog,我一直在看Android AlertDialog,它很容易使用setItems(…)添加要显示的字符串列表 但是,在大多数情况下,您需要一个显示漂亮字符串的列表,但是当从列表中选择某个内容时,您需要的是实际值,而不是字符串 我一直找不到如何以一种简单而好的方式做到这一点 提示?=) final Button Button1=(按钮)findviewbyd(R.id.Button1); Button1.setOnClickListener(新的OnClickListener() { @凌驾 公共v

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

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

我一直找不到如何以一种简单而好的方式做到这一点

提示?=)

final Button Button1=(按钮)findviewbyd(R.id.Button1);
Button1.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
final CharSequence[]items={“string1”、“string2”、“string3”};
//我想要的不是字符串数组,而是:
//ArrayList测试=新的ArrayList(myArray);
//CustomObject有一个toString()和一个值。这个数组应该是下面列表的基础=)
AlertDialog.Builder=新建AlertDialog.Builder(MainActivity.this);
setTitle(LanguageHandler.GetString(“Test”);
setItems(items,新的DialogInterface.OnClickListener()对话框){
公共void onClick(对话框接口对话框,int项){
//***我想在这里获取值***
Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert=builder.create();
alert.show();
}
});

而不是
CharSequence[]项={“字符串1”、“字符串2”、“字符串3”}
您可以在警报对话框中使用
自定义适配器

大概

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();
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;
    }
};
您的列表\u 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>

您的列表适配器类似

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();
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;
    }
};
String[]items={“飞机”、“动物”、“汽车”、“颜色”、“花朵”、“字母”、“怪物”、“数字”、“形状”、“笑脸”、“运动”、“星星”};
//在这里,您还可以对自定义对象使用ArrayList,而不是String[]项。。
ListAdapter=新阵列适配器(
getApplicationContext(),R.layout.list_行,项){
视窗座;
可绘制图标;
类视图持有者{
图像视图图标;
文本视图标题;
}
公共视图getView(int位置,视图转换视图,
视图组(父级){
最终LayoutFlater充气器=(LayoutFlater)getApplicationContext()
.getSystemService(
上下文。布局(充气机和服务);
if(convertView==null){
convertView=充气机。充气(
R.layout.list_行,空);
holder=新的ViewHolder();
holder.icon=(图像视图)convertView
.findviewbyd(R.id.icon);
holder.title=(TextView)convertView
.findviewbyd(R.id.title);
convertView.setTag(支架);
}否则{
//视图已定义,检索视图持有者
holder=(ViewHolder)convertView.getTag();
}       
Drawable Drawable=getResources().getDrawable(R.Drawable.list_图标);//这是drawables文件夹中的图像
持有者.头衔.setText(项目[位置]);
支架。图标。可设置图像可绘制(可绘制);
返回视图;
}
};

你能再多加些硼吗?你想要什么?嘿,对不起。直到现在我才看到这个评论。嗯,我认为上面的注释(即代码中的注释)有点清楚。我希望将自定义对象添加到AlertDialog,在该对话框中应打印objects toString()-方法,但单击时,我希望返回单击的对象…=)看看我的答案,希望你现在得到你想要的。。只需为您的对象创建自定义适配器,并将其设置为您的AlertDialog..@Ted,如果您有时间:。