gpt4 book ai didi

android - AlertDialog 列表中的自定义对象;如何获取显示字符串然后获取实际值?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:49 26 4
gpt4 key购买 nike

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

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

我一直无法找到如何以一种简单而漂亮的方式做到这一点。

提示? =)

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

最佳答案

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

有点像,

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

关于android - AlertDialog 列表中的自定义对象;如何获取显示字符串然后获取实际值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com