Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package voronin.kirill.util;
  2.  
  3. import android.widget.BaseAdapter;
  4. import android.content.Context;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import android.view.LayoutInflater;
  10. import android.content.res.Resources;
  11. import android.view.Menu;
  12. import android.util.Log;
  13.  
  14. public class IconizedMenuAdapter extends BaseAdapter {
  15.     private Context context;
  16.     private Resources res;
  17.     public String[] labels, icons;
  18.     private int menuLabelResource, menuIconResource, inflateView;
  19.  
  20.     public IconizedMenuAdapter (Context context, int inflateView, int menuLabelResource, int menuIconResource, int labelsResource, int iconsResource) {
  21.         this.context = context;
  22.         this.inflateView = inflateView;
  23.         this.menuLabelResource = menuLabelResource;
  24.         this.menuIconResource = menuIconResource;
  25.         res = context.getResources();
  26.  
  27.         labels = res.getStringArray(labelsResource);
  28.         icons = res.getStringArray(iconsResource);
  29.     }
  30.  
  31.     public Object getItem (int position) {
  32.         return position;
  33.     }
  34.  
  35.     public long getItemId (int position) {
  36.         return position;
  37.     }
  38.  
  39.     public int getCount () {
  40.         return labels.length;
  41.     }
  42.  
  43.     public View getView (int position, View convertView, ViewGroup parent) {
  44.         View view = convertView;
  45.         if (view == null) {
  46.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  47.             view = inflater.inflate(inflateView, null);
  48.         }
  49.  
  50.         TextView label = (TextView) view.findViewById(menuLabelResource);
  51.         ImageView icon = (ImageView) view.findViewById(menuIconResource);
  52.  
  53.         label.setText(labels[position]);
  54.         if (position < icons.length && icons[position] != "")
  55.             icon.setImageDrawable(
  56.                 res.getDrawable(
  57.                     res.getIdentifier(icons[position], "drawable", context.getPackageName()
  58.             )));
  59.  
  60.         return view;
  61.     }
  62. }
Add Comment
Please, Sign In to add comment