I am trying to load a list of images from an array. I got the images in list. Now I need to add a text for every image in the list. Can someone help me with this? I am new to Android.
public class EfficientAdapter extends BaseAdapter {
public EfficientAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_END);
i.setLayoutParams(new ListView.LayoutParams(60, 60));
return i;
}
private Context mContext;
private Integer[] mImageIds = { R.drawable.video3, R.drawable.video5, R.drawable.music2, };
}
This is my code where I have loaded list of images from an array. Please help me with this code.
You could do something like
Where you have also defined an array of Strings to hold the names of the images, perhaps like
It would be even easier if you create a layout for the ListItem and load that to create the view instead
Create a layout called say “mylistview.xml”
You could then make the
getView()method like thisThat will not only make life easier by allowing you design the view in XML but it will also be more efficient because you will re recycling views that have gone off screen but are still in memory because you are grabbing the
convertViewinstance when there is one.