settheline
settheline

Reputation: 3383

Android ListView in AlertDialog - setting adapter

I'm trying to implement a ListView in an AlertDialog in my app.

I call listDialog() after a button is clicked:

public void listDialog() {
        LayoutInflater shoppingListInflater = LayoutInflater.from(this);
        final View allListsView = shoppingListInflater.inflate(R.layout.list_picker_dialog, null);

        listsPickerView = (ListView) findViewById(R.id.shopping_lists_all);

        shoppingLists = new ArrayList<ShoppingList>();

        allListsAdapter = new ShoppingListsAdapter(this, shoppingLists);

        listsPickerView.setAdapter(allListsAdapter);

        AlertDialog.Builder listsDialog = new AlertDialog.Builder(this);

        listsDialog.setView(allListsView);

        listsDialog.setCancelable(false)
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

            AlertDialog alertD = listsDialog.create();

            alertD.show();

    }

In my Activity I have:

private ListView listsPickerView;
private ShoppingListsAdapter allListsAdapter;
private List<ShoppingList> shoppingLists;

My adapter looks like this:

public class ShoppingListsAdapter extends BaseAdapter {  
    private Activity activity;
    private LayoutInflater inflater;
    private List<ShoppingList> shoppingLists;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public ShoppingListsAdapter(Activity activity, List<ShoppingList> shoppingLists) {
        this.activity = activity;
        this.shoppingLists = shoppingLists;
    }

    @Override
    public int getCount() {
        return shoppingLists.size();
    }

    @Override
    public Object getItem(int location) {
        return shoppingLists.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.shopping_list, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        TextView name = (TextView) convertView
                .findViewById(R.id.listName);
        TextView listCount = (TextView) convertView
                .findViewById(R.id.listCount);
        TextView list_id = (TextView) convertView
                .findViewById(R.id.list_id);


        ShoppingList sList = shoppingLists.get(position);

        // set list count
        listCount.setText(sList.getListCount());

        //set name
        name.setText(sList.getName());

        list_id.setText(String.valueOf(sList.getId()));

        return convertView;
    }

}

My list_picker_dialog layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <ListView
        android:id="@+id/shopping_lists_all"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"/>

</LinearLayout>

I'm getting a NPE at this line: listsPickerView.setAdapter(allListsAdapter); in my listsDialog() method. Is there something wrong with my implemenation of ListView in the Dialog? Thanks in advance.

Upvotes: 0

Views: 781

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Since, presumably, your ListView is in the AlertDialog's layout, you need to look for it in the View inflated for the Dialog. Like so:

listsPickerView = (ListView) allListsView.findViewById(R.id.shopping_lists_all);

The reason you're getting a NullPointerException is because your code, as it is written, is searching for the ListView in the Activity's layout, and returning null when it doesn't find it.

Upvotes: 1

Related Questions