In my app I have a GridView inside ViewPager.
I am not using anywhere match_parent or wrap_content. I am using values like 200dp or 150dp. So why is my getView() of GridView adapter is calling multiple times?
I searched a lot like here,
here, here, and here.
This is my adapter class.
public class CustomGridViewAdapter1 extends BaseAdapter {
ArrayList<Integer> list2 = new ArrayList<Integer>();
private LayoutInflater mInflater;
public CustomGridViewAdapter1(Context context, ArrayList<Integer> list2) {
mInflater = LayoutInflater
.from(context.getApplicationContext());
this.list2 = list2;
}
#Override
public int getCount() {
return 6;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
View hView = convertView;
if (convertView == null) {
hView = mInflater.inflate(R.layout.greeditem, null);
ViewHolder1 holder = new ViewHolder1();
holder.song_pic = (ImageView) hView.findViewById(R.id.pic1);
holder.name = (TextView) hView
.findViewById(R.id.tabletext1);
holder.layout = (LinearLayout) hView
.findViewById(R.id.bingo_rlayout1);
hView.setTag(holder);
}
ViewHolder1 holder = (ViewHolder1) hView.getTag();
imageLoader.DisplayImage(list1.get(position), holder.song_pic);
holder.name.setText(list.get(position));
if (list2.get(position) == 1) {
holder.layout.setBackgroundColor(getActivity()
.getResources().getColor(R.color.lightblue));
holder.name.setTextColor(getActivity().getResources().getColor(R.color.white));
}
return hView;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
And this is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:layout_width="320dp"
android:layout_height="250dp"
android:layout_below="#+id/ticketText"
android:numColumns="2"
android:background="#fff"
android:listSelector="#android:color/white">
</GridView>
<TextView
android:id="#+id/scoreId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/ticketText"
android:layout_alignBottom="#+id/ticketText"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:text="Score 1/6"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:id="#+id/ticketText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tablayout"
android:layout_marginLeft="16dp"
android:text="My Ticket #1"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
We need to check the null condition in getView(). Try with below code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
ObjectItem objectItem = data[position];
// assign values if the object is not null
if(objectItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.textViewItem.setText(objectItem.itemName);
viewHolder.textViewItem.setTag(objectItem.itemId);
}
return convertView;
}
Related
I have Listview that contains a Textview, I populate data from sqlite inside them , now I want to change the textview background color for each row based on the value in each one.
I couldn't do it , can anybody help me for how to get each textview from the listview ??
Edit :
Here is my code to populate the listview:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Group, SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.grop, R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
this is the XML file for the items :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/grp_shap" >
<ImageView android:id="#+id/Prio"
android:layout_width="32dip"
android:layout_height="32dip"
/>
<TextView
android:id="#+id/grop"
android:layout_width="64sp"
android:layout_height="40sp"
android:layout_alignBottom="#+id/Prio"
android:layout_alignParentLeft="true"
android:text="S"
android:textSize="12sp" />
<TextView
android:id="#+id/DT"
android:layout_width="wrap_content"
android:layout_height="14sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/title"
android:gravity="right"
android:text="Date Rec"
android:layout_marginRight="5dip"
android:textSize="12sp"
/>
<TextView
android:id="#+id/bdy"
android:layout_width="fill_parent"
android:layout_height="28sp"
android:layout_below="#+id/DT"
android:layout_toLeftOf="#+id/hi"
android:layout_toRightOf="#+id/Prio"
android:text="Body"
android:textSize="12sp" />
<TextView
android:id="#+id/sbj"
android:layout_width="fill_parent"
android:layout_height="12sp"
android:layout_alignLeft="#+id/bdy"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/bdy"
android:fontFamily="sans-serif"
android:text="Subject"
android:textSize="12sp" />
<ImageView
android:id="#+id/hi"
android:layout_width="20dip"
android:layout_height="26dip"
android:layout_alignLeft="#+id/DT"
android:layout_below="#+id/DT"
android:layout_marginLeft="15dp"
android:src="#drawable/highprio" />
</RelativeLayout>
and I have a listview on the activity_mail layout which populated whith this items.
I want to change the color for the textview with id grop based on the value inserted on it
You should use a custom adapter:
public class MyAdapter extends BaseAdapter {
private static class ViewHolder {
public final TextView mTv;
}
private final Context mContext;
private final List<String> mList; //you can change this to String array
public MyAdapter (Context context, List<String> list) {
this.mContext = context;
this.mList= list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
final String string = mList.get(position);
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_listview, null);
h = new ViewHolder();
h.mTv= (TextView) convertView .findViewById(R.id.mTvRes);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.mTv.setText....
h.mTv.setTextColor....
h.mTv.setBackground....
return convertView;
}
}
in your activity:
List<String> list = Arrays.asList(Fields);
MyAdapter adapter = new MyAdapter(this, list);
NotiLst.setAdapter(adapter);
I didn't checked it and you maybe need to do some changes but it should work.
If you have something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder.label.setBackground(getResources().getColor(color.white));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.label.setText(activitiesList.get(position).getName());
return convertView;
}
I have simple list which suppose to display all the rows from DB table. However, it shows only one row in the list view.
Though, it receives all the rows from the DB (verified the size of configArrayList), but displays only the first row and discards others.
Here is the Activity and Custom Adapter.
Main Activity :
ArrayList<HashMap<String, String>> configArrayList = dbTools.getAllConfig(); // Returns 4 rows
ArrayList<ConfigEntity> configMenu = new ArrayList<>();
if (!(configArrayList.isEmpty()))
{
Iterator<HashMap<String, String>> iterator = configArrayList.iterator();
while (iterator.hasNext())
{
HashMap<String, String> configMap = iterator.next();
// ConfigEntity contains two members Name, ID and their setter, getter
ConfigEntity entity = new ConfigEntity();
entity.setID(configMap.get("ID"));
entity.setName(String.valueOf(configArrayList.size()));
// Set Name as Size to verify the list size is grater than 1. (Size is 4)
configMenu.add(entity);
}
}
if (!(configMenu.isEmpty()))
{
ListAdapter adapter = new ConfigListAdapter(this, configMenu);
ListView theListView = (ListView) findViewById(R.id.config_list_listView);
theListView.setAdapter(adapter);
}
Here is the Custom Adapter :
public class ConfigListAdapter extends BaseAdapter {
private ArrayList<ConfigEntity> entityList;
private LayoutInflater layoutInflater;
public ConfigListAdapter(Context context, ArrayList<ConfigEntity> entityList) {
this.entityList = entityList;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return entityList.size();
}
#Override
public Object getItem(int position) {
return entityList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) convertView.findViewById(R.id.config_list_row_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView idTextView;
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:id="#+id/config_list_addConfig_button"
android:layout_gravity="right"
android:onClick="addNewConfig" />
<ListView
android:id="#+id/config_list_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp">
</ListView>
</LinearLayout>
</ScrollView>
config_list_listView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin_20dp">
android:layout_marginRight="#dimen/margin_20dp">
<TextView
android:id="#+id/config_list_row_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="#dimen/margin_10dp"
android:layout_marginLeft="#dimen/margin_20dp"
android:longClickable="true" />
<TextView
android:id="#+id/config_list_row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Please try this.
Change getView Method as follows
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=convertView;
ViewHolder holder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) view.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) view.findViewById(R.id.config_list_row_id);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return view;
}
and Declare View view; as class level variable;
Hope it helps Thanks. Refer here :-https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/
I'm trying to create a inflated dialog which contains dynamic amount of items.
These Items I inflate aswell.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int anzJobViews = getArguments().getInt("param");
View rootView = inflater.inflate(R.layout.dialog_job_chooser, container, false);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ListView listview = (ListView) rootView.findViewById(R.id.dialog_chooser_listview);
for (int i = 0; i < anzJobViews; i++) {
listview.addFooterView( new JobItemView(context));
Log.i("JobChooser","Item added");
}
return rootView;
}
The View (Contains some simple TextViews)
private void init() {
inflate(getContext(), R.layout.job_item_view, this);
}
When showing the dialog I see Nothing.
The xml layout for the dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:background="#drawable/round_corners_selected"
android:backgroundTint="#color/washedOutGreen">
<ListView
android:id="#+id/dialog_chooser_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
The Items a want to add to the List.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue_focused">
<TextView
android:id="#+id/tv_jobname_area"
android:text="defaultName"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_jobvalue_area"
android:text="0 /h"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/tv_jobname_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Why don't use an adapter to populate your listview? An example from here: http://www.pcsalt.com/android/listview-using-baseadapter-android/
public MyBaseAdapter(Context context, ArrayList myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.tvTitle.setText(currentListData.getTitle());
mViewHolder.tvDesc.setText(currentListData.getDescription());
mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());
return convertView;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
public MyViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.tvTitle);
tvDesc = (TextView) item.findViewById(R.id.tvDesc);
ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
}
}
}
Also, if you are in a pre kitkat enviroment, you can only use addFooterView before setting the adapter.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Reference: http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
use getView function to setup your listview
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(context, R.layout.items, null);
}
else {
element = convertView;
}
//setup here something
//e.g.
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText("myText");
return element;
}
How can I create something like this?
I want to have the "selected image" as the main view, and then a time line effect at the bottom; something like a Gallery would work, but that has been deprecated.
I'm thinking a HorizontalScrollView at the bottom, but will that recycle the views properly? Can I implement the ViewHolder pattern here?
What about StackView? I found this: http://www.gdgankara.org/2012/03/25/stackview-non-widget-sample/ which could be cool, but I couldn't find a lot of things about StackViews online, so I'm wondering how much it gets implemented?
I solved this problem by using a custom horizontal ListView by MUKESH YADAV
I changed his HorizontalImageAdapter.java class to use a CursorAdapter.
My solution here:
public class HorizontalImageAdapter extends CursorAdapter {
//...some initialization here
public HorizontalImageAdapter(Context context, Cursor c, int count) {
super(context, c, true);
this.context = (Activity) context;
this.count = count;
this.cursor = c;
inflater = LayoutInflater.from(context);
cursor.moveToFirst();
}
private static class ViewHolder {
public ImageView imageview;
public TextView textview;
public ImageView imageviewvideo;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
cursor.moveToPosition(position);
if (convertView == null) {
Log.d(TAG, "converView == null <<<<<<<<<<<<<<<<<<<<<<<<");
convertView = inflater.inflate(R.layout.activity_media_items, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.tv_details_title);
holder.imageview = (ImageView) convertView.findViewById(R.id.iv_details_resource_image);
holder.imageviewvideo = (ImageView) convertView.findViewById(R.id.iv_details_resource_video);
convertView.setTag(holder);
} else {
Log.d(TAG, "converView != null >>>>>>>>>>>>>>>>>>>>>>>>");
}
//get the type of the item
type = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_ITEM_TYPE));
if(type.equalsIgnoreCase("text")){
//handle for text item
}
if(type.equalsIgnoreCase("image")){
//handle for image item
}
if(type.equalsIgnoreCase("video")){
//handle for video item
}
return convertView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_TITLE));
String fileName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View eachitem = inflater.inflate(R.layout.activity_media_items, parent, false);
holder.textview = (TextView) eachgrid.findViewById(R.id.tv_details_title);
holder.imageview = (ImageView) eachgrid.findViewById(R.id.iv_details_resource_image);
holder.imageviewvideo = (ImageView) eachgrid.findViewById(R.id.iv_details_resource_video);
eachgrid.setTag(holder);
return eachitem;
}
}
And the XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/black" >
<ImageView
android:id="#+id/selected_imageview"
android:layout_width="#dimen/exhibit_item_width"
android:layout_height="#dimen/exhibit_item_height"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:maxHeight="#dimen/exhibit_item_height"
android:maxWidth="#dimen/exhibit_item_width"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/gallery_relative_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/exhibit_items_height"
android:layout_gravity="bottom"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<com.example.testdatabase2.HorizontalView
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="#dimen/exhibit_items_height"
android:layout_centerHorizontal="true"
android:smoothScrollbar="true"
android:spacing="20dip" >
</com.example.testdatabase2.HorizontalView >
</RelativeLayout>
</LinearLayout>
I successfully created a gridview populated with images. Now, what I want to do is put a text below the image.
This is the screenshot of my app so far.
http://i203.photobucket.com/albums/aa266/paulocarabuena_bucket/screen-2.png
Here is my getView method in my imageadapter class..
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(this.context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(this.thumbs[position]);
return imageView;
}
Here is the onCreate method of my main activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.launchFullScreen();
this.setContentView(R.layout.main);
this.grids = (GridView) this.findViewById(R.id.elements);
ImageAdapter adapter = new ImageAdapter(this);
this.grids.setAdapter(adapter);
this.grids.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
Thanks in advance. :)
Instead of Directly using Image View , you should inflate a view as below :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColorHighlight="#656565">
</TextView>
</LinearLayout>
And your Adapter should be like this :
public class ImageAdapter extends BaseAdapter{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
}
You can do it using an XML, that defines the GridView cell:
image.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/image"
android:layout_marginBottom = "10dp"
android:src="#drawable/cell_sfondo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Then in your getView():
if(convertView == null) {
view = inflater.inflate(R.layout.image, null);
}
else
view = convertView;
ImageView image = (ImageView)view.findViewById(R.id.image);
image.setImageResource(this.thumbs[position]);
Paresh Mayani have a really good and simple example. This helped me a lot.
http://www.technotalkative.com/android-gridview-example/
You can create a layout for your image and text in each cell of the grid layout. And I suggest inflating the layout from an xml. You can try to do something like this:
//Use a viewHolder to optimize the list
ViewHolder holder = null;
if (convertView == null) {
//Create a new view holder to optimize the loading of elements in list
holder = new ViewHolder();
convertView = LayoutInflater.from(this.context).inflate(R.layout.my_custom_xml,null) ;
holder.imageView = (ImageView)convertView.findViewByID(R.id.my_image_view);
holder.textView = (TextView)convertView.findViewByID(R.id.my_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
//Bind data to the row
//Set the position in holder
holder.position = position;
//Set the image for each row
holder.imageView ...
//Set the text for each row
holder.textView.setText()...
//And the viewholder looks like this
private class ViewHolder{
//The position of this row in list
private int position;
//The image view for each row
private ImageView imageView;
//The textView for each row
private TextView textView;
}
And now you can simply create my_custom_xml which contain the text and image view:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height = "wrap_content"
android:orientation= "vertical">
<ImageView
android:id = "#+id/my_image_view"
android:layout_width = ....
android:layout_height = ...
/>
<TextView
android:id = "#+id/my_text_view"
android:layout_width = ....
android:layout_height = ....
/>
</LinearLayout>
Instead of taking dynamic imageview,you can inflate layout having textview under imageview in adapter as:
if(convertView==null){
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder=new Holder();
convertView=inflater.inflate(R.layout.gridinflater, null);
holder.imageView=(ImageView) convertView.findViewById(R.id.img);
holder.textView=(TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
where gridInflater is layout having textview under imageview in Relative Layout as parent.
public static class Holder {
public ImageViewProgress imageView;
public TextView textView;
}