i have started to learn android development. i am making an app in which i want to show movie thumbnails(image view and text view) combined in grid view layout i have made custo
You should not inflate the main activity layout in your adapter. Instead you should have a layout of the individual row to inflate in your getView()
method.
What you have to do is, You have to give adapter a list of data you want to set in gridview. Adapter inflates the rows (the individual item in Grid) according to the count of data(models) given, then you will have to assign the data to the individual elements of the row.
So, I have corrected the adapter. Hope this will be helpful:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater inflater;
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
private String mTexts[]={"a","b","d",....,"your texts"};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length; // adapter inflates the row according to the count of data given
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return mThumbIds.length;// inflates rows according to data given
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
NewHolder holder = null;
ImageView imageView;
if(convertView==null){//if convert view is null then only inflate the row
inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = inflater.inflate(R.layout.grid_item_layout,viewGroup,false);
holder = new NewHolder();
//find views in item row
holder.imageView = (ImageView)convertView.findViewById(R.id.imageview_id);
holder.textView = (ImageView)convertView.findViewById(R.id.textview_id);
convertView.setTag(holder);
}
else { //otherwise get holder from tag
holder = (NewHolder) convertView.getTag();
}
//set data here
holder.imageView.setImageResource(mThumbIds[position]);
holder.textView.setText(mTexts[position]);
return convertView;
}
public class NewHolder {
public ImageView imageView;
public TextView textView;
}
}
and your grid_item.xml should have layout for each item in GridView like: