Articles → Android → Gridview Control In Android
Gridview Control In Android
What Is Gridview Control?
Example
- Create an adapter for gridview.
package com.example.demo_application;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class GridAdapter extends BaseAdapter {
private Context mContext;
String[] strArray = new String[] {
"Name 1",
"Name 2",
"Name 3",
"Name 4"
};
public GridAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return strArray.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView textView;
if (view == null) {
textView = new TextView(mContext);
} else {
textView = (TextView) view;
}
textView.setText(strArray[i]);
return textView;
}
}
- Bind grid using adapter
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));
Click to Enlarge