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