gabi
gabi

Reputation: 1113

Radio alert dialog with custom row layout

I have AlertDialog, building it like this:

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

and it works great, But now I need to change it to fill with 2 values each line.
for example name and price.
I want to create XML layout with LinearLayout, orientation="horizontal", and 2 TextViews under it, But I cannot figure out how to use it with the builder and how to fill the Strings into the TextView.

I tried to read all the examples and posts here but it not fit what I need..

Thanks.

Upvotes: 0

Views: 643

Answers (1)

You can make a Custom Dialog with your custom Layout. Here is a good tutorial:

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

Edit: Then you have to create your own row layout and custom ListView.

Check these solutions: How can I display a list view in an Android Alert Dialog?

You can create custom adapters like this:

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

}

Upvotes: 1

Related Questions