Reputation: 31
I did a Gridview using BaseAdapter, but I am having a problem.
When I am scrolling the Gridview up and down, on the third or fourth time the Gridview will be deleted and I am left with an empty screen.
Here My Code
Adapterclass
public class BooksAdapter extends BaseAdapter
{
String bookscategoryList[] =
{
"The World",
"Food & Drink",
"Crime, Mystery & Thriller",
"Science Fiction & Fantasy",
"Family & Health",
"Music, Stage & Screen",
"Language and Linguistics",
"Fiction",
"Business & Finance",
"Time Periods in History",
"Special Interest Books",
"Romance & Erotica",
"Children's Books",
"Biography",
"Mind Body & Spirit",
"Lifestyle, Sport & Leisure",
"Sports",
"Society & Social Sciences",
"Art, Architecture and Photography",
"History & Transport","Medical",
"Religion & Spirituality",
"Literature & Literary Studies",
"Earth Sciences, Geography, Environment & Planning",
"Graphic Novels",
"Humour",
"Natural History & Pets",
"Hobbies & Games",
"Education"
};
int imagescategory[]=
{
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};
Context ctx;
public BooksAdapter(Context context)
{
this.ctx = context;
}
@Override
public int getCount() {
return imagescategory.length;
}
@Override
public Object getItem(int position) {
return imagescategory.length;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater)ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = inflater.inflate(R.layout.category_list_row, null);
holder = new ViewHolder();
holder.tvCategories = (TextView) convertView.findViewById(R.id.tvCategory);
holder.ivCategories = (ImageView) convertView.findViewById(R.id.ivCategories);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tvCategories.setText(bookscategoryList[position]);
holder.ivCategories.setImageResource(imagescategory[position]);
return convertView;
}
static class ViewHolder {
private TextView tvCategories;
private ImageView ivCategories;
}
}
My Actvity class
public class CategoryActivity extends Activity {
ImageView ivCategory;
GridView gvCategory;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_layout);
gvCategory = (GridView) findViewById(R.id.gvCategories);
BooksAdapter booksAdapter = new BooksAdapter(CategoryActivity.this);
gvCategory.setAdapter(booksAdapter);
}
Upvotes: 0
Views: 71
Reputation: 163
public class CustomAdapter extends BaseAdapter {
Context context;
ImageLoader loader;
ArrayList<HashMap<String, String>> itemlist = new ArrayList<HashMap<String, String>>();
public CustomAdapter(Context context,
ArrayList<HashMap<String, String>> itemlist) {
// TODO Auto-generated constructor stub
this.itemlist = itemlist;
this.context = context;
loader = new ImageLoader(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemlist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemlist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.product_item, parent, false);
ImageView img = (ImageView) view.findViewById(R.id.img_item);
TextView tv = (TextView) view.findViewById(R.id.text_item);
Utility.setFont(tv, ProductList.this);
Display display = ((Activity) ProductList.this).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.d("check", "width "+width);
Log.d("check", "height "+height);
view.getLayoutParams().height=(int)(height/4);
view.getLayoutParams().width=(int)width/2;
loader.DisplayImage(Utility.IMAGE_URL+itemlist.get(position).get("image"),
(Activity) context, img);
tv.setText(itemlist.get(position).get("product_name"));
return view;
}
}
Upvotes: 3