适用于Android中GridView的自定义适配器

时间:2011-03-16 11:39:06

标签: android gridview onclick

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

我的代码:

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

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

2 个答案:

答案 0 :(得分:8)

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

好的啧啧:

Custom GridView

答案 1 :(得分:1)

对GridView使用SimpleAdapter。这是设计每个项目视图的最佳方式。

例如, http://wptrafficanalyzer.in/blog/listing-images-in-gridview-using-simple-adapter-in-android/