简体   繁体   English

GridView在Android中使用BaseAdapter?

[英]GridView using BaseAdapter in Android?

I did a Gridview using BaseAdapter, but I am having a problem. 我使用BaseAdapter进行了Gridview,但是出现问题。

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. 当我上下滚动Gridview时,第三次或第四次Gridview将被删除,而我却没有任何显示。

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);

}
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;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM