Well, I solved the problem, but I'm wondering if there might be a more
elegant solution.
Perhaps I should go back a couple of steps. Basically, I'm trying to
modify a ListView/SimpleCursorAdapter combo. I've "binded" my
ListAdapter to the query by extending SimpleCursorAdapter. I then
changed the viewBinder for this custom SimpleCursorAdapter class to
another custom class that implements SimpleCursorAdapter.ViewBinder. I
did this because I want to manipulate one of my layout objects based
on one of the query column values (i.e.: if value is this, use this
icon; if value is that, user other icon; etc.).
Here's my original code, which had some problems:
private void fillData() {
Cursor c = myDB.fetchMyData();
startManagingCursor(c);
String[] from = new String[] {"column1", "column2", "user"};
int[] to = new int[] {R.id.timeStamp, R.id.myText,
R.id.friendName};
myCursorAdapter notes;
notes = new myCursorAdapter(this, c, from, to);
setListAdapter(notes);
}
class myCursorAdapter extends SimpleCursorAdapter {
public myCursorAdapter(Context context, Cursor c,
String[] from, int[] to) {
super(context, R.layout.myLayout, c, from, to);
// TODO Auto-generated constructor stub
setViewBinder(new myViewBinder());
}
}
public class myViewBinder implements
SimpleCursorAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int
columnIndex) {
// TODO Auto-generated method stub
int nUserIndex = cursor.getColumnIndex("user");
int user_id =
Integer.parseInt(cursor.getString(nUserIndex));
if(nUserIndex == columnIndex) {
TextView friendNameView = (TextView)
view.findViewById
(R.id.friendName);
ImageView iconView = (ImageView)
view.findViewById(R.id.icon);
// query database and get more info
about this user
Cursor userCursor = myDB.fetchUserInfo(user_id);
userCursor.moveToFirst();
String name =
userCursor.getString(userCursor.getColumnIndexOrThrow
("name"));
friendNameView.setText(name);
/* Some code to do stuff with the
ImageView for R.id.icon */
userCursor.close();
return true;
}
return false;
}
}
So, here was the problem. From the code above you can see that I'd
never get the object for R.id.icon, since it wasn't binded to the
custom SimpleClassAdapter!
To fix it, I just modified the binding so that R.id.icon tied to a
column from the query that I wasn't using:
String[] from = new String[] {"created_at", "text", "user",
"_id"};
int[] to = new int[] {R.id.timeStamp, R.id.myText,
R.id.friendName, R.id.icon};
Then modified my ViewBinder to look for that column and modify the
icon as necessary:
int n_idIndex = cursor.getColumnIndex("_id");
if(n_idIndex == columnIndex) {
ImageView iconView = (ImageView) view.findViewById(R.id.icon);
/* yada, yada, yada */
}
That works great, and I'm happy with the end result. I'm just stuck
with this feeling that there must be a more elegant solution to fixing
this problem. As I'm still somewhat new to the Android environment,
I'd appreciate any feedback/advice. Thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---