-1

i am currently using an string array in which i have to put icons for the items in navigation drawer.Here is my code

  <string-array name="drawer_titles">
    <item >About Us</item>
    <item>FeedBack</item>
    <item>Setting</item>
    <item>Share App</item>
    <item>Rate Us</item>
    <item>Logout</item>

  how can i add list icons to the adapter which contains list items

    image=(ImageView) findViewById(R.id.image) ;
    t1=(TextView) findViewById(R.id.t1) ;
    t2=(TextView) findViewById(R.id.t2) ;
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,      R.layout.drawer_list_item, mDrawerItmes));
Anusha Dandu
  • 41
  • 1
  • 8
  • check this:: http://stackoverflow.com/questions/32573800/android-how-to-add-icon-on-each-listview-list-item-and-change-the-text-color-bac – Sagar Chavada Sep 29 '16 at 05:12

5 Answers5

6

create an string array in strings.xml like this one, <integer-array name="navigation_drawables_values"> <item>@drawable/my_post_normal</item> <item>@drawable/my_engagement_icon_normal</item> <item>@drawable/my_payment_icon_normal</item> <item>@drawable/setting_icon_normal</item> <item>@drawable/about_icon_normal</item> <item>@drawable/supprot_icon_normal</item> </integer-array>

Then access in the activity or fragament like this one,

TypedArray imgs =    getResources().obtainTypedArray(R.array.navigation_drawables_values);
imgs.getResourceId(i, -1)
 mImgView1.setImageResource(imgs.getResourceId(i, -1));
imgs.recycle();

Thanks to @Patrick Kafka answers

Community
  • 1
  • 1
Reprator
  • 2,859
  • 2
  • 32
  • 55
1

Place your icons in mipmap folder and add their resource id's in string-array as follows

<string-array name="icon_array">
    <item>@mipmap/ic_about_us</item>
    <item>@mipmap/ic_shopping</item>
    <item>@mipmap/icon_dining</item>
    <item>@mipmap/ic_my_city</item>
    <item>@mipmap/ic_health_fitness</item>
    <item>@mipmap/icon_education</item>
</string-array>

get them in your Activity as

TypedArray icons = context.getResources().getStringArray(R.array.discover_city_text_array);

pass that icons array to adapter and set them based on their position

Logic
  • 2,230
  • 2
  • 24
  • 41
1

Make an integer array that contains your drawables and pass that array to your adapter.

int[] iconArray={R.drawable.icon1,R.drawable.icon2,.....};
AbhayBohra
  • 2,047
  • 24
  • 36
0

In my example I used Picasso to load images into ImageView.

Create your own implementation of ArrayAdapter, something like this

       public class MyAdapter extends ArrayAdapter<DataModel> {

            private Context mContext;
            private Integer mResourceID;

            public MyAdapter(Context context, int resource, ) {
                super(context, resource);
                this.mContext = context;
                this.mResourceID = resource;
            }

            @Override
            public int getCount() {
                // DATA_ARRAY your data
                return DATA_ARRAY.size();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if(v == null)
                    v = LayoutInflater.from(this.mContext).inflate(this.mResourceID,parent,false);

                DataModel model = DATA_ARRAY.get(position)

                ((TextView) v.findViewById(R.id.my_text_view)).setText(model.getStringData());
                ImageView imgIcon =  (ImageView)v.findViewById(R.id.my_image_icon);  
                // Load your icon here depending on the data in DATA_ARRAY.get(position)
                // Or you can include an int property in your DataModel which contains the icon resource
                Picasso.with(this.mContext).load(R.mipmap.ic_twitter).into(imgIcon);

                return v;
            }
        }
JustADev
  • 258
  • 1
  • 8
  • 19
0
ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.image1);
array_image.add(R.drawable.image2);
Jaydroid
  • 334
  • 3
  • 13