Glide messes up gridlayout - android

I'm trying to make a grid view to dynamically load images and data from a web server, but if i try to use Glide to load the image it messes up my layout, im a newbie on android programming so please be patient
the correct layout is this:
but if i enable Glide it shows like this:
As you can see there is a wide gap at the bottom that shouldn't be there
Here is my Grid Adapter:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
ArrayList<HashMap<String, String>> hotelsList;
TextView hotelNameTv;
TextView hotelDistanceTv;
ImageView hotelImage;
public GridViewAdapter(Context c, ArrayList<HashMap<String, String>> hotelsList ) {
mContext = c;
this.hotelsList = hotelsList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return hotelsList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return hotelsList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.hotel_item, null);
hotelNameTv = (TextView) grid.findViewById(R.id.hotelName);
hotelDistanceTv = (TextView) grid.findViewById(R.id.hotelDistance);
hotelImage = (ImageView) grid.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelNameTv.setText(hotel.get("name"));
hotelDistanceTv.setText(hotel.get("distancemsg"));
/*
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
*/
} else {
grid = (View) convertView;
}
return grid;
}
}
Here is my Activity Main resource File:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/windowBackground">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<GridView
android:id="#+id/hotelsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="#dimen/small_margin"
android:horizontalSpacing="#dimen/small_margin"
android:stretchMode="columnWidth"
android:numColumns="2"
android:padding="10dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And my item resource file
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="30dp"
>
<TextView
android:text="Hotel Name"
android:layout_width="match_parent"
android:layout_height="56sp"
android:gravity="center_vertical"
android:id="#+id/hotelName"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textColor="#color/colorPrimary"
android:paddingLeft="5sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/hotel"
android:id="#+id/hotelImage"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:cropToPadding="false"
android:layout_below="#+id/hotelName"
android:layout_alignParentStart="true" />
<TextView
android:text="Hotel Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hotelDistance"
android:background="#222222"
android:textAppearance="#android:style/TextAppearance.Material.Inverse"
android:layout_below="#+id/hotelImage"
android:layout_alignParentStart="true"
android:padding="5sp"
android:textColor="#color/gold" />
</RelativeLayout>
</RelativeLayout>
Thanks for your help!!!

Not sure what i did... first i changed my get view to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
And it didn't worked...
But then i changed from glide to picasso, it worked perfectly...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Picasso.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
I'll be very grateful if someone explains me what happened...

You can check this link below. Gridview in Android is quite studpid. (I suggest you use Recyclerview with GridLayoutManager)
How to have a GridView that adapts its height when items are added

Related

Scrolling GridView's items make GridView scrolls up down and will be gone

I request you just see this video you will know what is the issue
https://www.youtube.com/watch?v=gsZSMRPEZSI&feature=youtu.be
Gridview scrolls and Gone when I scroll it's item
This issue is not occurring when there is limited no. of items in GridView or when we scroll slowly
I have taken Custom GridView, in that I am loading images and text from server
below is the code
GridView's XML file
<?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:background="#color/background" >
<TextView
android:id="#+id/edtTxtProfileQuotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/blue_notification_shape"
android:drawableLeft="#drawable/blue_notification_icon"
android:drawablePadding="#dimen/small_margin"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/light_violet" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="#drawable/teambg" />
<GridView
android:id="#+id/grdGrupOfUnite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
android:layout_below="#id/edtTxtProfileQuotes"
android:numColumns="3" >
</GridView>
</RelativeLayout>
Adapter's xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" >
<com.example.achessapp.widget.CircularImageView
android:id="#+id/ivUniteGrpMemIcon"
android:layout_width="#dimen/icon_size"
android:layout_height="#dimen/icon_size"
android:layout_centerHorizontal="true"
android:src="#android:color/white" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ivUniteGrpMemIcon"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/extra_small_margin"
android:layout_marginBottom="#dimen/small_margin"
android:gravity="center_horizontal"
android:textColor="#android:color/black" />
</RelativeLayout>
in Activity File
adapter = new BondMemberAdapter(mActivity, list_members_bean);
mGridMembers.setAdapter(adapter);
In Adapter file
public class BondMemberAdapter extends BaseAdapter {
LayoutInflater mInflator;
Activity mActivity;
private ArrayList<MembersBean> list_members_bean;
Typeface tf = null;
public BondMemberAdapter(Activity activity, ArrayList<MembersBean> list) {
mActivity = activity;
mInflator = LayoutInflater.from(mActivity);
list_members_bean = list;
tf = Typeface.createFromAsset(
mActivity.getAssets(), Constants.FONT_TYPE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list_members_bean.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MembersBean bean = list_members_bean.get(position);
if (convertView == null) {
convertView = mInflator.inflate(R.layout.unite_grup_member_item,
null);
viewHolder = new ViewHolder();
viewHolder.ivUser = (CircularImageView) convertView
.findViewById(R.id.ivUniteGrpMemIcon);
viewHolder.tvName = (TextView) convertView
.findViewById(R.id.tvName);
viewHolder.tvName.setTypeface(tf);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String text = "";
text = bean.getScreen_name();
//viewHolder.ivUser.setImageResource(R.drawable.unite_item);
if (bean.getThumbnail_url() != null) {
if (bean.getThumbnail_url().equals("null")) {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
} else {
ImageLoader.getInstance().displayImage(bean.getThumbnail_url(),
viewHolder.ivUser);
}
}
else {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
}
viewHolder.tvName.setText(text);
return convertView;
}
static class ViewHolder {
CircularImageView ivUser;
TextView tvName;
int position;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_members_bean.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}

Android - Optimize GridView Scrolling

I have a fragment that contains a GridView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:padding="20dp"
android:id="#+id/sub_category_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="20dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:fastScrollEnabled="true"
tools:listitem="#layout/sub_category_view" />
</LinearLayout>
Every child of this GridView contains one TextView and one ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/sub_category_view">
<ir.motorel.carbuyinstruduction.SquareLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/picture1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="10dp"
android:id="#+id/sub_category_img"/>
</ir.motorel.carbuyinstruduction.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#C36518"
android:id="#+id/sub_category_name"/>
</LinearLayout>
When I fill GridView with Texts and Images, it's become very Laggy and slow when I scrolling. This is My adapter Class:
public class CustomGrid extends BaseAdapter{
private Context mContext;
public CustomGrid(Context c) {
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return subCatTitle.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
viewHolder = new ViewHolder();
viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
//subCatImg.setImageResource(imgIDs.getResourceId(position, -1));
convertView.setTag(viewHolder);
}
else{
//imgIDs.recycle();
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.subCatTitle.setText(subCatTitle[position]);
return convertView;
}
}
public class ViewHolder{
public TextView subCatTitle;
public ImageView subCatImg;
public LinearLayout subCatView;
}
How can i optimize it to scroll more smoothly?
PS. My images are in drawable folder and their format is PNG.
I had a ImageView in my Activity's Header that I set a really big image to it. It was killing performance. There is no problem in GridView.

Android Adding Dynamic ListView Header

I have a list view and I am trying to set header for it so that it looks like a table header. Here is the XML file of my list view row:
PS: I have removed some codes to shorten the question
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
And my XML file of my header list view:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Category" />
And the class where I populate the list view:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.trans_header_layout, null);
listview.addHeaderView(listHeaderView);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.trans_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
However, by using these codes, here is the output that I've gotten:
The header does not match with the column below it. I wonder is there any alternate way to add header to list view so that it looks like a table header.
Thanks in advance.
EDIT
My transaction_rec.XML where the listview is located:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
And my transactionRec.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_rec);
context = this;
listview = (ListView) findViewById(R.id.listview);
//Code to get data from database
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_rec,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
}
And the output I've gotten now:
try this...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
with all text views.

Display data vertically in ListView

I have created an android application in that I want to display some Image data vertically in ListView.
How to make it possible ?
I tried using this code-
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_above="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout1"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:layout_gravity="fill_vertical"
android:entries="#array/country_image_list" >
</ListView>
</LinearLayout>
public class ItemImagesAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> paths;
private ImageLoader iml;
public ItemImagesAdapter(FragmentActivity activity, ArrayList<String> image_paths) {
context = activity;
paths = image_paths;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
ImageLoader.getInstance().init(config);
iml = ImageLoader.getInstance();
}
#Override
public int getCount() {
return paths.size();
}
#Override
public Object getItem(int position) {
return paths.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
}
String path = paths.get(position);
ImageView image = (ImageView) convertView.findViewById(R.id.image1);
iml.displayImage(path, image);
return convertView;
}
}
take one more layout for list item as R.layout.list_item_image.
here i am taking image loader to load the images from server.
if you are getting images by means local...then set them directly
hope this may helps you
You can try to manage your requirement using TwoWayView for Horizontal scrolling of ListView items with Vertical scrolling. You can aslo check this demo for the same on github.
XML for first activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
XML for row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Create this layouts and then using custom adapter class you can fill the images for each row in image view...Something like this
Custom Adapter class
public class customAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String,String>>();
Activity activity;
private static LayoutInflater inflater = null;
public customAdapter(Activity a, ArrayList<HashMap<String, String>> al) {
Log.e("check", "");
activity = a;
this.al = al;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
imageLoader=new ImageLoader(activity.getApplicationContext());
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.row_layout, null);
}
ImageView myImage = (ImageView) vi.findViewById(R.id.myImage);
imageLoader.DisplayImage(//your image from resource here),myImage);
return vi;
}
}
Any problem in this you can follow the link of the tutorial I have mentioned in the comments...

Custom GridAdapter with 2 columns doesn't works

I want to implement a grid Adapter that receives an ArrayList of Element2S, where Element2S is a custom object with two String (Element2S(String a, String b)
The GridAdapter should use the Element2S ArrayList to populate the Grid.
the first String of Element2S object should be the first column element, the second String of Element2S the second element
My GridAdapter is:
public class GridViewAdapterC2 extends BaseAdapter {
private ArrayList<Element2S> itemList;
private Activity activity;
public GridViewAdapterC2(Activity activity,
ArrayList<Element2S> itemList) {
super();
this.itemList = itemList;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Element2S getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public TextView txtViewColumn1;
public TextView txtViewColumn2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_row_2c, null);
view.txtViewColumn1 = (TextView) convertView
.findViewById(R.id.col1);
view.txtViewColumn2 = (TextView) convertView
.findViewById(R.id.col2);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
String textCoilumn1 = itemList.get(position).getA();
String textCoilumn2 = itemList.get(position).getB();
view.txtViewColumn1.setText(textCoilumn1);
view.txtViewColumn2.setText(textCoilumn2);
return convertView;
}
}
Unfortunately when in the main I try to use this adapter with
gridAdapter = new GridViewAdapterC2(this, myItemArrayList);
mygrid= (GridView) findViewById(R.id.myGridView);
mygrid.setAdapter(gridAdapter);
The result is wrong.
My gridRow layout is
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_gravity="left">
<TextView
android:id="#+id/col1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left|start"
android:padding="5dp"
android:textColor="#color/light_sky"
android:textSize="12sp" />
<TextView
android:id="#+id/col2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left|start"
android:padding="5dp"
android:textColor="#color/green_2"
android:textSize="12sp" >
</TextView>
</TableRow>
</TableLayout>
The myGridView layout in the main activity is
<GridView
android:id="#+id/myGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:gravity="center_horizontal"
android:numColumns="2" >
</GridView>
I cannot figure what is wrong.
If my Elements2S arraylist is
1st element2S: (1,23a)
2nd element2S: (2,15b)
3rd element2S: (3,22c)
The Grid shows
1 2
3
But the right output is
1 23a
2 15b
3 22c
I think the ListView is better than GridView in this situation.
And I think the LinearLayout is better than TableLayout.
Your output is wrong because some region of your grid's cell is covered by others. Try to resize your cell and grid.

Categories

Resources