0

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.

gabi
  • 1,003
  • 5
  • 12
  • 30

1 Answers1

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

}
Community
  • 1
  • 1
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • already read it.. I need the Dialog to be list of Radio lists of some kind, if I will follow this example I will need to implement everything, lists , Radio buttons, selection etc.. I want to be able to use something that exists, but change the row layout.. – gabi Jan 11 '15 at 20:56
  • 1
    Then you have to create your own row layout and custom ListView. Check these solutions: http://stackoverflow.com/questions/15762905/how-to-display-list-view-in-alert-dialog-in-android – Oğuzhan Döngül Jan 11 '15 at 21:02
  • Please edit your answer and add it, I will mark it as answer. – gabi Jan 11 '15 at 21:05
  • On your link, I see the example, but it is LiatAdapter of Strings, what if I need ListAdapter of Objects to fill 2 Strings, Name and Price for example.. – gabi Jan 11 '15 at 21:07
  • @gabi Edited my answer you can check. – Oğuzhan Döngül Jan 11 '15 at 21:18
  • Trying it now. Thanks! – gabi Jan 11 '15 at 21:19
  • Works, but How can I add and handle the radio button? on my example I had "builder.setSingleChoiceItems(items, checked, this);" which handled it (also I saved "chacked" var for the last checked position.. – gabi Jan 11 '15 at 21:35
  • You need to set OnItemClickListener to your Listview then you can handle clicks – Oğuzhan Döngül Jan 11 '15 at 21:47
  • The only thing is that I do not have the Radio buttons on the dialog.. :/ – gabi Jan 11 '15 at 21:58