Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom adapter for GridView in Android

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?

My code:

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;
    }
}
like image 915
sanjay Avatar asked Mar 16 '11 11:03

sanjay


People also ask

Which adapter is used to fill the data in GridView?

In android commonly used adapters which fill data in GridView are: ArrayAdapter. BaseAdapter.

What is difference between ArrayAdapter and BaseAdapter in Android?

Here is the difference: BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working. ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s.

What is an ArrayAdapter in Android?

ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView.

What is GridView layout in Android?

A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView.


2 Answers

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.

Nice Tut:

Custom GridView

like image 149
Blundell Avatar answered Oct 18 '22 13:10

Blundell


Use SimpleAdapter for GridView. This is the best way to design view of each item.

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

like image 30
user1575120 Avatar answered Oct 18 '22 13:10

user1575120