【问题标题】:Custom adapter for GridView in AndroidAndroid中GridView的自定义适配器
【发布时间】:2011-03-16 11:39:05
【问题描述】:

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

我的代码:

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

我的自定义列表适配器:

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

【问题讨论】:

  • 请您添加R.layout.listview1的xml文件,这样我可以理解你的例子,我也有类似的情况。

标签: android gridview onclick


【解决方案1】:

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

不错的图:

Custom GridView

【讨论】:

  • 感谢您的回复..我已经用我的 CustomListAdapter 代码更新了我的问题..它也扩展了 BaseAdapter..我可以在哪里添加这些按钮?
  • 链接中的教程非常过时 - 对于那些像我一样没有注意到其 2011 年答案的人来说;)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多