Android 中 GridView 的自定义适配器 [英] Custom adapter for GridView in Android

查看:32
本文介绍了Android 中 GridView 的自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库获取数据并显示在 GridView 中.但是我需要在显示的每个文本下方放置一个单独的按钮.当我点击按钮时,我必须做一些事情.在这里,我使用自定义列表适配器从 DB 检索数据.我怎么能这样做?

I'm getting data from DB and displayed in a GridView fine. But I need to put a separate button below each text displayed. When I click the button, I have to do some stuff. Here I used a custom list adapter for retrieved data from DB. How could I do that?

我的代码:

public class HomePage extends Activity  {
    private ArrayList<SingleElementDetails> allElementDetails=new ArrayList<SingleElementDetails>();
    DBAdapter db=new DBAdapter(this);
    String category, description;
    String data;
    String data1;
    GridView gridview;
    Button  menu;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);

        menu=(Button)findViewById(R.id.menus);

        menu.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                gridview=(GridView)findViewById(R.id.gridview);
                allElementDetails.clear();
                db.open();
                long id;
                //id=db1.insertTitle1(category, description,r_photo);
                Cursor cursor = db.getAllTitles1();
                while (cursor.moveToNext())
                {
                    SingleElementDetails single=new SingleElementDetails();
                    single.setCateogry(cursor.getString(1));
                    single.setDescription(cursor.getString(2));
                    single.setImage(cursor.getBlob(3));
                    allElementDetails.add(single);
                }
                db.close();
                CustomListAdapter adapter=new CustomListAdapter(HomePage.this,allElementDetails);
                gridview.setAdapter(adapter);
            }
        });
    }
}

我的自定义列表适配器:

My customListAdapter:

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {
    private  ArrayList<SingleElementDetails> allElementDetails;

    private LayoutInflater mInflater;

    public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
        allElementDetails = results;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return allElementDetails.size();        
    }

    public Object getItem(int position) {
        return allElementDetails.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        convertView = mInflater.inflate(R.layout.listview1, null);
        ImageView imageview = (ImageView) convertView.findViewById(R.id.image);
        TextView textview = (TextView) convertView.findViewById(R.id.category_entry);
        TextView textview1 = (TextView) convertView.findViewById(R.id.description_entry);
        textview.setText(allElementDetails.get(position).getCategory());
        textview1.setText(allElementDetails.get(position).getDescription());

        byte[] byteimage=allElementDetails.get(position).getImage();
        ByteArrayInputStream imageStream = new ByteArrayInputStream(byteimage);
        BitmapFactory.Options op=new BitmapFactory.Options();
        op.inSampleSize=12;
        Bitmap theImage= BitmapFactory.decodeStream(imageStream,null,op);
        imageview.setImageBitmap(theImage);
        return convertView;
    }
}

推荐答案

您必须创建自己的适配器来扩展 BaseAdapter,为此类中的每个网格项(扩展 LinearLayout)创建一个布局,而不是使用 CustomListAdaptertextview 然后是一个按钮.

Instead of using CustomListAdapter, you will have to create your own adapter that extends BaseAdapter, create a layout for each grid item (extend LinearLayout) in this class have your textview then a button.

不错的啧啧:

自定义GridView

这篇关于Android 中 GridView 的自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆