How to use listview in Dialog ?

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.

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

5.CustomAdapter should be like this.

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

}

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

Advertisements

One thought on “How to use listview in Dialog ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s