0

I need help designing an Adapter for my GridView. I have an StringArray inside my strings.xml with about 100 items. I have this so far, but Im not sure what to put in the constructor to link my StringArray to this Adapter.

public class Tab01_FavAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;

    public Tab01_FavAdapter (Context c) {
        mContext = c;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.griditemlayout, parent, false);
        }

        ... 

        return convertView;
    }
}

2 Answers2

1

If I have not missunderstood, you could pass the dataset as paramter, for instance

ArrayList<String> dataset;
public Tab01_FavAdapter (Context c, ArrayList<String> dataset) {
        mContext = c;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.dataset = dataset;

    }


String[] stringArray =    getResources().getStringArray(R.array.FullList); 
ArrayList<String> dataset = new ArrayList<String>(Arrays.asList(stringArray)) 
Blackbelt
  • 148,780
  • 26
  • 271
  • 285
  • Couldn't I do something like: `ArrayList FullList = R.array.FullList;` –  Jul 16 '13 at 15:07
  • 1
    Yes He can, but imo is better ArrayList – Blackbelt Jul 16 '13 at 15:11
  • @Extremis is that inside a activity class? – Raghunandan Jul 16 '13 at 15:15
  • where did you run it ? You need a Context. Run it inside an Activity, or use mContex.getResources().getStringArray – Blackbelt Jul 16 '13 at 15:15
  • @Extremis http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster. check this list vs arrays. i would prefer lists.http://robaustin.wikidot.com/how-does-the-performance-of-arraylist-compare-to-array 100 items performance does not matte here i guess coz items are not large. – Raghunandan Jul 16 '13 at 15:25
1

You can do as below apart from the arraylist as suggested by blackbelt

  String s[] = getResources().getStringArray(R.array.StringArray);
  new Tab01_FavAdapter(this,s); 

getResources() requires activty context. Make sure you use the above inside a activity class. Or use ActivityContext from the activity to call getResources

If you have a fragment you can use getActivity() to get the context of the hosting activity.

Receive the string array in custructor

 String s[];      
 public Tab01_FavAdapter (Context c,String[] arr ) {
    mContext = c;
    mInflator = (LayoutInflater)    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    s= arr; 
}

@Override
public int getCount() {
    return s.length;
}

Use the same in getView

Raghunandan
  • 129,147
  • 24
  • 216
  • 249