[英]Radio alert dialog with custom row layout
我有AlertDialog,以如下方式构建它:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
CharSequence[] filters = {"a", "b", "c", "d"};
builder.setTitle("Title");
builder.setSingleChoiceItems(items, checked, this);
filter_dialog = builder.create();
而且效果很好,但是现在我需要更改它,以每行填充2个值。
例如名称和价格。
我想创建带有LinearLayout,orientation =“ horizontal”和下面有2个TextViews的XML布局,但是我不知道如何在构建器中使用它以及如何将字符串填充到TextView中。
我试图在这里阅读所有示例和帖子,但是不适合我的需求。
谢谢。
您可以使用自定义布局创建自定义对话框。 这是一个很好的教程:
http://www.mkyong.com/android/android-custom-dialog-example/
编辑:然后,您必须创建自己的行布局和自定义ListView。
检查以下解决方案: 如何在Android Alert对话框中显示列表视图?
您可以像这样创建自定义适配器:
public class CustomAdapter extends ArrayAdapter<CustomObject> {
Context context;
int layoutResourceId;
ArrayList<CustomObject> data = null;
public CustomAdapter (Context context, int layoutResourceId, ArrayList<CustomObject> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class ViewHolder
{
ImageView imgDealImage;
TextView txtDescription;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imgDealImage = (ImageView)convertView.findViewById(R.id.imgDealImage);
holder.txtDescription = (TextView)convertView.findViewById(R.id.txtDescription);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
int image = data.get(position).getImage();
String description = data.get(position).getDescription();
holder.imgDealImage.setImageResource(image);
holder.txtDescription.setText(description);
return convertView;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.