سؤال

I am implementing a navigation Drawer as per: http://developer.android.com/training/implementing-navigation/nav-drawer.html

The tutorial only shows a single TextView with ID android:id="@android:id/text1" and this is populated for the ListView using:

mDrawerList.setAdapter(new ArrayAdapter<String>(
            this,
            R.layout.drawer_list_item,
            mDrawerItems));

Now, I want to include an ImageView inside each ListView item. This will represent the Icon for each item.

Question: Inside my xml for the list items (where the TextView text1 is located), I have included an ImageView.

  1. What do I need to name the ID for the ImageView?

  2. And then, do I simply add the drawable-array reference to the setAdapter like this?

    mDrawerList.setAdapter(new ArrayAdapter<String>(
            this,
            R.layout.drawer_list_item,
            mDrawerItems,
            mDrawerIcons));
    

Thank you

هل كانت مفيدة؟

المحلول

I found out that creating an ArrayAdapter in the format above does not allow you to input more than one set of data to the Adapter. For instance, I can only use my list of Drawer Navigation names contained in the Array<String>: mDrawerItems. To pass Icons and other data to the adapter, I have two option:

  1. Simple Adapter used with a Map, or

  2. Custom Adapter.

Examples of both can be found here: http://www.vogella.com/articles/AndroidListView/article.html, and here:

http://www.shenhengbin.wordpress.com/2012/03/17/listview-simpleadapter/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top