I'm a beginner in android development. In the application that i'm working on , data is being displayed from a database to a list view using the default simple cursor adapter(not subclassing it).
I want to add the option to display the data in a grid view as well , if the user desires.
I checked out some examples in the web and it seemed identical to the list view. So again im fetching the data from the database using a simple cursor adapter and displaying it on the grid view.
All of the items are being displaying correctly at first , that is until i begin scrolling it. Everything gets mixed up then.
Each grid view item has 2 textviews, the first one displaying its title and the second one it's description. So, when scrolling, descriptions get mixed up , item titles with the wrong descriptions etc. Also sometimes the entire grid view dissapears completely.
I never had such issues with my listview , it always worked as intended.
What am i missing here? Would appreciate your insight.
UPDATE :
The grid view dissappearing when scrolling was due to grid view items being uneven in height.
It seems that my other issue lies within the viewbinder that i've set for the adapter. If i comment out the following code , i no longer get mixups between grid view items. Here's the viewbinder code :
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 3) {
String color = aCursor.getString(aColumnIndex);
aView.setBackgroundColor(Color.parseColor(color));
return true;
}
if (aColumnIndex == 4) {
if (aCursor.getString(5)!=null) {
ViewGroup parentview = (ViewGroup)aView.getParent();
parentview.removeAllViews();
TableLayout tablelayout = new TableLayout (MainActivity.this);
parentview.addView(tablelayout);
int count = Integer.parseInt(aCursor.getString(5));
String[] texts = convertStringToArray(aCursor.getString(4));
String[] cb = convertStringToArray(aCursor.getString(6));
for (int i =0;i<count;i++) {
LayoutInflater inflater = getLayoutInflater();
View checklist_row = inflater.inflate(R.layout.widget_row_layout, null);
TextView tv = (TextView)checklist_row.findViewById(R.id.textView1);
ImageView im = (ImageView)checklist_row.findViewById(R.id.imageView1);
if (cb[i].equalsIgnoreCase("true"))
im.setBackgroundResource(R.drawable.cbc);
tv.setText(texts[i]);
parentview.addView(checklist_row);
}
return true;
}
else {
TextView tv = (TextView) aView.findViewById(R.id.textView2);
tv.setText(aCursor.getString(4));
return true;
}
}
return false;
}
});
Any ideas on how to fix this?