java - 具有自定义行布局的 radio 警报对话框

标签 java android 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、orientation="horizo​​ntal"和其下的 2 个 TextView 创建 XML 布局,但我不知道如何将它与构建器一起使用以及如何将字符串填充到 TextView 中。

我尝试阅读此处的所有示例和帖子,但它不符合我的需要..

谢谢。

最佳答案

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

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

}

关于java - 具有自定义行布局的 radio 警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27891860/

相关文章:

Android SDK 不允许下载 api 8 和 10

java - 是否有使用完整百分比编码在 Android 中编码/解码字符串的快速功能?

android - 自定义 AlertDialog 未显示

android - 如何为对话框设置最大高度?

java - 如何检查哪些复选框已选中,哪些未选中?

java - 视频驱动程序不支持 OpenGL

javascript - 如何隐藏 Microsoft Office Online Viewer 中的状态栏

android - 无法播放 Assets "it is probably compressed"中的音频

Java SAX 解析器,如何完全防止字符引用? (DoS 攻击)

java - 未装饰按钮 : JButton or JLabel with MouseListener?