How can I get three TextView values? - android

My listview has three textview and one checkbox in horizontal order.
My listview xml code
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView1"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView2"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView3"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="5" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkBox1"
android:focusable="false"
android:clickable="false"
android:layout_gravity="right"
android:layout_marginRight="10dp"/>
and MainActivity.java code
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View view, int position, long id) {
Object vo = (Object)adapterView.getAdapter().getItem(position);
Log.d(TAG, String.valueOf(vo));
}
});
The log output for String.valueOf(vo) is
sn.studyroom.ListViewItem#f085546
How can I get each of the three Textview values?

If you catch the clicked object right, you must convert it to TextView, and then get your TextView's text like this:
String vo = ((TextView)adapterView.getAdapter().getItem(position)).getText().toString();
Log.d(TAG, vo);
OR
Go your adapter class and create an interface to get clicked item's values.

You can use adapter for accessing your text fields and check box. Pass your list to a adapter like this:
CustomListAdapter adapter = new CustomListAdapter (context, yourList);
yourListView.setAdapter(adapter);
Create an adapter like this.
public class CustomListAdapter extends BaseAdapter {
Context context;
List<ListObject> yourList;
public CustomListAdapter (Context context, List<ListObject> yourList) {
this.context = context;
this.yourList= yourList;
}
public void refreshAdapter(List<ListObject> yourList){
this.yourList= yourList;
notifyDataSetChanged();
}
private static class ViewHolder {
TextView textView1, textView2, textView3;
CheckBox checkBox1;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return yourList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return yourList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.your_adapter_layout, null);
holder = new ViewHolder();
holder.textView1= convertView.findViewById(R.id.textView1);
holder.textView2= convertView.findViewById(R.id.textView2);
holder.textView3= convertView.findViewById(R.id.textView3);
holder.checkBox1= convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//TODO: you can do whatever you want to do here
return convertView;
}
}

If you want to retrive list data then i want to know about to set data. Are you set model and add in adapter or taking static string arraylist? so let me know then i will suggest better way to do that.

Related

Button inside Grid layout is null

I am creating custom grid. Each row has 1 button and 2 textviews. I am trying to assign values/text to each programmatically. I am able to set text for both the text views but, findviewbyId for button returns null.
Grid is like:
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#FFFFFF"
android:padding="5dip"
android:id="#+id/gvMYActivitySearch" />
Row layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnHistory"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="#drawable/button_background"
android:text="History"/>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="35dp" />
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="35dp" />
</LinearLayout>
Custom grid java file is like
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] names;
private final String[] Contact;
private final String[] ID;
public CustomGrid(Context c,String[] names,String[] Contact, String[] ID ) {
mContext = c;
this.Contact = Contact;
this.names = names;
this.ID = ID;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.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
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//grid = new View(mContext);
grid = inflater.inflate(R.layout.myactivity_singlerow, null);
} else {
grid = (View) convertView;
}
TextView tvName = (TextView) grid.findViewById(R.id.tvName);
TextView tvContact = (TextView) grid.findViewById(R.id.tvContact);
Button btnHistory = (Button) grid.findViewById(R.id.btnHistory);
tvName.setText(names[position]);
tvContact.setText(Contact[position]);
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().testButton(view.getId());
}
}
);
}
return grid;
}
When I try to debug, I can see for the first time when position is '0', btnHistory is not null. But after that when position is 1, btnHistory is null.
What could possibly go wrong? When both the textviews are not null, why btnHistory is null?
Comment to improve your code:
callback somewhere outside your getView
I mean this:
public class CustomGrid extends BaseAdapter implements OnClickListener {
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setTag((Integer)view.getId());
btnHistory.setOnClickListener(this);
}
public void onClick(View v) {
new MainActivity().testButton(v.getTag());
}
}

Android Track Positions in a custom ListView without itemclicklistener

I have a ListView with custom list items using a BaseAdapter and ViewHolder. Each list item has two Buttons, and a TextView. I have set click listeners for both buttons. Now the onItemClickEvent for the ListView does not work. I think the buttons consume the click event or something similar.
I can find out which button was clicked, but I can't get the row position,
as the ViewHolder is recycled. I cant do this without ViewHolder` as there is high possibility of the number of list items can become very large.
How can i get the row position when a button is clicked .
I tried this after doing some googling , But still there is no change in the out come.
Could someone give any suggestions.
Below is my ListItem Layout and BaseAdapter
ListItem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/llv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Large Text"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
BaseAdapter
class MyBaseAdapter extends BaseAdapter
{
LayoutInflater li;
Context context;
public MyBaseAdapter(Context context)
{
this.context=context;
li=LayoutInflater.from(context);
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 10;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO u-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
holder=new ViewHolder();
convertView=li.inflate(R.layout.listitem, parent,false);
holder.iv1=(ImageView) convertView.findViewById(R.id.imageView1);
ImageView iv=(ImageView) convertView.findViewById(R.id.imageView2);
iv.setTag(position);
holder.iv2=iv;
holder.tv1=(TextView) convertView.findViewById(R.id.textView1);
holder.pos=position;
holder.iv1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(context, "IV1 "+holder.pos,
Toast.LENGTH_SHORT).show();
}
});
holder.iv2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x=(int) v.getTag();
Toast.makeText(context, "IV2 "+x,
Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
}
else
{
holder=(ViewHolder) convertView.getTag();
}
holder.tv1.setText("LIST ITEM "+position);
return convertView;
}
class ViewHolder
{
TextView tv1;
ImageView iv1,iv2;
int pos;
}
}
You have to set position every time.
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView=li.inflate(R.layout.listitem, parent,false);
holder.iv1=(ImageView) convertView.findViewById(R.id.imageView1);
holder.iv2 =(ImageView) convertView.findViewById(R.id.imageView2);
holder.iv1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// get position from tag.
int selection = (int) v.getTag();
Toast.makeText(context, "IV1 "+selection,
Toast.LENGTH_SHORT).show();
}
});
...
} else {
holder = (ViewHolder) convertView.getTag();
}
// set position.
holder.v1.setTag(position);
...

How to create Button Object which is in another layout xml file in Android

Hello I have create an Android application, in that I add some buttons and images in ListView using CustomAdapter.
My Files Structure is:
layout
[1] album ->List View
[2] album_list -> Button
So how I create Button Object ?
My Code is:
album.xml
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0" >
</ListView>
</GridLayout>
album_list.xml
<RelativeLayout
android:id="#+id/RelativeLayout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:layout_alignTop="#+id/btn_hindi"
android:layout_alignBottom="#+id/ImageView12"
android:layout_alignLeft="#+id/ImageView12"
android:background="#drawable/normal"
android:text="ENG"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/tv_albumname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_go"
android:layout_alignTop="#+id/iv_album_image"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="Album Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/white" />
<Button
android:id="#+id/btn_hindi"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginTop="7dp"
android:layout_marginRight="17dp"
android:layout_alignTop="#+id/ImageView12"
android:layout_alignLeft="#+id/btn_eng"
android:background="#drawable/normal"
android:text="HINDI"
android:textColor="#android:color/white" />
</RelativeLayout>
Album.java
Album_List_Custom_Adapter adapter =
new Album_List_Custom_Adapter(getApplicationContext(), albumList);
lv.setAdapter(adapter);
Album_List_Custom_Adapter.java
private Context context;
ArrayList<HashMap<String, String>> listAlbum;
ViewHolder vholder;
private OnClickListener listener;
public Album_List_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList)
{
this.context = context;
this.listAlbum=albumList;
}
#Override
public int getCount()
{
return listAlbum.size();
}
#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)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi=convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.home_list_model, null);
vholder = new ViewHolder();
vholder.hindi=(Button)vi.findViewById(R.id.btn_hindi);
vholder.eng=(Button)vi.findViewById(R.id.btn_eng);
vholder.tv_album_name = (TextView) vi.findViewById(R.id.tv_albumname);
vi.setTag(vholder);
}
else
{
vholder = (ViewHolder) (vi.getTag());
}
vholder.hindi.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
vholder.hindi.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.hindi.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.eng.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.hindi.setBackgroundResource(R.drawable.normal);
}
});
vholder.tv_album_name.setText(listAlbum.get(position).get("album"));
return vi;
}
static class ViewHolder
{
TextView tv_album_name;
Button eng, hindi;
}
You have to create button object in adapter where u r inflating list item layout.
like
Button b = (Button)yourViewobject.findViewById(R.id.button1);
Now implement onClick event for that
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
EDIT:
Use this. Make one custom file in drawable folder named mybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected" android:state_selected="true"></item>
<item android:drawable="#drawable/normal"></item>
</selector>
Now just in your custom layout file set background for your both buttons.
android:background="#drawable/mybutton" // for Hindi Button
and same as for english button.
And also now remove onClick event method from your adapter.
Need to change..
<Button
android:id="#+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:clickable = "true"
android:focusable = "true"
android:layout_alignTop="#+id/btn_hindi"
android:layout_alignBottom="#+id/ImageView12"
android:layout_alignLeft="#+id/ImageView12"
android:background="#drawable/mybutton"
android:text="ENG"
android:textColor="#android:color/white" />
Do this in your custom adapter
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
viewHolder=new ViewHolderforGroup();
viewHolder.btn = (Button)convertView.findViewById(R.id.btn);
convertView.setTag(viewHolder)
}else{
viewHolder = (ViewHolder)convertView.getTag(viewHolder);
}
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
static class ViewHolder{
Button btn;
}
public class CustomAdapter extends BaseAdapter
{
private Context mContext;
Cursor cursor;
public CustomAdapter(Context context,Cursor cur)
{
super();
mContext=context;
cursor=cur;
}
public int getCount()
{
// return the number of records in cursor
return cursor.getCount();
}
// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent)
{
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listview_each_item, null);
// fetch the sender number and sms body from cursor
String Number="123456";
// get the reference of textViews
TextView Number=(TextView)view.findViewById(R.id.textViewSMSSender);
Button btn=(Button)view.findViewById(R.id.button);
// Set the Sender number and smsBody to respective TextViews
Number.setText(senderNumber);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return view;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}

CheckBox in custom ListView on Android

I got a custom ListView, and I put three TextView in the List using a custom List Adapter! How I can to add a CheckBox with each List Item and make each CheckBox checked on list item click?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#7a4b9d"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/calorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<TextView
android:id="#+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/chkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
MainActivity
public class Main_Activity extends Fragment implements OnClickListener {
private ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main_activity, null);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) root.findViewById(R.id.list_two);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setTextFilterEnabled(true);
lv.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
Toast.makeText(getActivity(),"You have chosen: " + " " +
fullObject.getPhone(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
private ArrayList<SearchResults> GetSearchResults() {
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setName("Apple");
sr.setCalorie("35");
sr.setPrice("5");
results.add(sr);
return results;
}
}
Custom Base Adapter
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCalorie = (TextView) convertView
.findViewById(R.id.calorie);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.chkBox1 = (CheckBox)convertView.findViewById(R.id.chkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCalorie.setText(searchArrayList.get(position)
.getCalorie());
holder.txtPrice.setText(searchArrayList.get(position).getPrice());
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
return convertView;
}
static class ViewHolder {
CheckBox chkBox1;
TextView txtName;
TextView txtCalorie;
TextView txtPrice;
}
Search Results
public boolean isSolved() {
// TODO Auto-generated method stub
return b;
}
public void setSolved(boolean b) {
// TODO Auto-generated method stub
this.b = b;
}
You will have to define your CheckBox view in custom_row_view.xml file first,
<CheckBox android:id="#+id/chkBox1"
android:layout_width=...
android:layout_height=... />
Then, you wll have to call this reference in your MyCustomBaseAdapter class or in the holder in your case, like
CheckBox chkBox1;
then,
holder.chkBox1 = = (CheckBox)convertView.findViewById(R.id.chkBox1);
You can define a boolean value in your SearchResults class which can take care of chkBox check and use
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
Something on these lines and you will be good to go :)
EDIT: Remember to change the value of boolean in searchResult instance on itemClick.
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
lv.getAdapter().notifyDataSetChanged();

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