1.First declare AlertDialog reference
AlertDialog dialog;

2.Second thing is you have to create an instance for AlertDialog.Builder
AlertDialog.Builder builder=new AlertDialog.Builder(this);

3.Set the title whatever you want and Here important thing is
AlertDialog.Builder should set the view .
4.The following code is in onCreate Method.Here CustomListAdapter is Adapter class which extends BaseAdapter for the custom view.

[sourcecode language=”java”]
Button btn=(Button) findViewById(R.id.button1);
btn.setText("Listview in Dialog");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder=new AlertDialog.Builder(DialogWithListviewActivity.this);
builder.setTitle("Flowers");
ListView list=new ListView(DialogWithListviewActivity.this);
list.setAdapter(new CustomListAdapter(DialogWithListviewActivity.this));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
if(dialog.isShowing())
{
dialog.dismiss();
}
Toast.makeText(mContext, "Clicked at Position"+position, Toast.LENGTH_SHORT).show();
}
});
builder.setView(list);
dialog=builder.create();
dialog.show();
}
});
[/sourcecode]

5.CustomAdapter should be like this.

[sourcecode language=”java”]
<pre>public class CustomListAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater inflator;
final int[] images = { R.drawable.magnolia,
R.drawable.orchid, R.drawable.rose,
};
public CustomListAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mContext=context;
this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

MainListHolder mHolder;

View v = convertView;
if (convertView == null)
{
mHolder = new MainListHolder();

v = inflator.inflate(R.layout.inflate, null);
mHolder.image= (ImageView) v.findViewById(R.id.imageView1);

v.setTag(mHolder);
} else {
mHolder = (MainListHolder) v.getTag();
}

mHolder.image.setImageResource(images[position]);
mHolder.image.setPadding(20, 20, 20, 20);
return v;
}
class MainListHolder
{
private ImageView image;
}

}
[/sourcecode]

I hope that this is useful for someone and download example here

How to use listview in Dialog ?
Tagged on:         

0 thoughts on “How to use listview in Dialog ?

  • January 26, 2015 at 8:55 am
    Permalink

    It would be great if you shared the xml files too

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *