0

In my application i have an class extending dialog fragment. The class displays normal dialog(not Alert dialog).

1.displaying a listview inside dialog. the list view contains 2 text views , both are populated by 2 string arrays. I am confused about which adapter to use other than base adapters

2.how to inflate a layout for dialog. Is setView method available only for alert dialog

Manish Srivastava
  • 1,649
  • 2
  • 15
  • 23
user1526671
  • 817
  • 4
  • 16
  • 33

3 Answers3

1

The only way to use a layout in a Dialog is to subclass it:

class MyDialog extends Dialog{
public MyDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);

    String[] arr1 = new String[]{"title1", "title2", "title3"};
    String[] arr2 = new String[]{"content1", "content2", "content3"};

    ListView  list = (ListView)findViewById(R.id.list);
    list.setAdapter(new MyAdapter(arr1, arr2));
}

where R.layout.dialog is your layout where your listView is defined, which we will set a Custom Adapter, inflating a layout containing the two strings.

private final class MyAdapter extends BaseAdapter{
    private String[] mArray1;
    private String[] mArray2;

    public MyAdapter(String[] arr1, String[] arr2) {
        mArray1 = arr1;
        mArray2 = arr2;
    }

    public int getCount() { return mArray1.length; }
    public Object getItem(int position) { return mArray1[position]; }
    public long getItemId(int position) { return 0; }

    public View getView(int position, View convertView, ViewGroup parent) {
        String str1 = mArray1[position];
        String str2 = mArray2[position];
        if(convertView==null){
            LayoutInflater li = getLayoutInflater();
            convertView = li.inflate(R.layout.my_list_cell, null);
        }

        TextView text1 = (TextView)convertView.findViewById(R.id.text1);
        text1.setText(str1);

        TextView text2 = (TextView)convertView.findViewById(R.id.text2);
        text1.setText(str2);

        return convertView;
    }
}
Corbella
  • 1,791
  • 14
  • 24
0

If you want to display the data inside the dialog using an Adapter, you could use a ListAdapter. It could be something like this :

    ListAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.select_dialog_item, android.R.id.text1, items) {
      public View getView(int position, View convertView, ViewGroup parent) {

      // Do something here

        return view;
      }
    };
lokoko
  • 5,785
  • 5
  • 35
  • 68
0

which adapter to use other than base adapters

=> There is no restriction. You can use and customize any adapter as per your requirement. for example, you can use ArrayAdapter e.g. ArrayAdapter<JSONObject> or ArrayAdapter<String>.

How to inflate a layout for dialog. Is setView method available only

=> check: How can inflate a layout containing listview in a alert dialog?

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • but i am not using alert dialog ,have implemented dialogs. Is it needed to use base adapter...that is any other way without customizing adapter – user1526671 Feb 04 '13 at 09:41