I Still Can Not Figure Out Custom Cursor Adapters With Sqlite Results
I have an sqlite table with the following structure: String CREATE_ACHIEVEMENTS_TABLE = 'CREATE TABLE achievements (' + 'id INTEGER PRIMARY KEY,' + 'n
Solution 1:
Since I'm not able to reproduce your development environment this is all I could do to refactor your code:
publicclassShowAchievementsextendsListActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHandlerdb=newDatabaseHandler(this);
Cursorcursor= db.getAchievements(this);
MyCursorAdaptercursorAdapter=newMyCursorAdapter(this, cursor);
this.setListAdapter(cursorAdapter);
}
private Cursor getAchievements(Context context) {
StringselectQuery="SELECT * FROM achievements ORDER BY id asc";
SQLiteDatabasedb=this.getWritableDatabase();
return db.rawQuery(selectQuery, null);
}
privateclassMyCursorAdapterextendsCursorAdapter {
publicMyCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@OverridepublicvoidbindView(View v, Context context, Cursor cursor) {
if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes"){
TextViewtv= (TextView) v.findViewById(R.id.NAMEOFTEXTVIEW);
tv.setTextColor(Color.GREEN);
}
}
@Overridepublic View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
LayoutInflaterinflater= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.achievement_item, parent, false);
return row;
}
}
}
Please try it, fix the syntax errors if any and let me know how it works or not.
Solution 2:
just take space before the each (") for your sqlite code, because they bind each other and the code can't be execute. make it like this :
StringCREATE_ACHIEVEMENTS_TABLE="CREATE TABLE achievements ("
+ " id INTEGER PRIMARY KEY,"
+ " name VARCHAR,"
+ " type VARCHAR,"
+ " value VARCHAR,"
+ " completed VARCHAR"
+ ")";
Post a Comment for "I Still Can Not Figure Out Custom Cursor Adapters With Sqlite Results"