带自定义行布局的无线电警报对话框

时间:2015-01-11 20:42:11

标签: java android alertdialog layout-inflater

我有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创建XML布局,orientation =“horizo​​ntal”,下面有2个TextViews,但是我无法弄清楚如何将它与构建器一起使用以及如何将字符串填充到TextView中。

我试着在这里阅读所有的例子和帖子,但它不符合我的需要..

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用自定义布局制作自定义对话框。这是一个很好的教程:

http://www.mkyong.com/android/android-custom-dialog-example/

编辑:然后你必须创建自己的行布局和自定义ListView。

检查以下解决方案:How can I display a list view in an Android Alert Dialog?

您可以像这样创建自定义适配器:

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

}
相关问题