androidsqlitelistviewandroid-cursoradapter

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,"
             + "name VARCHAR,"
             + "type VARCHAR,"
             + "value VARCHAR,"
             + "completed VARCHAR"
             + ")";

now what i am trying to do is have it so that if the "completed" column is set to "yes", i want the text color of the row in my listview to be GREEN.

ive read tutorials and looked at code examples and i just cant figure out how to implement this with a custom cursoradapter. right now to display the results in a listview i use the following code:

ShowAchievements.java (this activity is started when a menu item "view achievements" is clicked):

package com.dd.qsg;

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ShowAchievements extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.achievements);

        DatabaseHandler db = new DatabaseHandler(this);
        ArrayList<String> achievements = db.getAchievements(this);

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, achievements));
    } 

}

the getAchievements() function that is called in the above activity:

public ArrayList<String> getAchievements(Context context) {
    ArrayList<String> achievementList = new ArrayList<String>();
    String selectQuery = "SELECT * FROM achievements ORDER BY id asc";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                if (cursor.getString(4).equals("yes")) {
                    achievementList.add(cursor.getString(1)+" (completed)");
                }
                else {
                    achievementList.add(cursor.getString(1));
                }
            } while (cursor.moveToNext());
        }
    }
    else {
        achievementList.add(context.getResources().getString(R.string.na));
    }

    cursor.close();
    db.close();
    return achievementList;
}

like i said i want to implement a custom cursoradapter so that when i call getAchievements() from within my ShowAchievements activity, it will make the color of the text GREEN in the listview row if "completed" is set to "yes" for that row in the database. i hope this makes sense.

in another question i asked someone helped me out, they told me to implement something like this:

private class MyCursorAdapter extends CursorAdapter {

    public MyCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        if(cursor.getString(cursor.getColumnIndex("completed").equals("yes")){
            TextView tv = (TextView) v.findViewById(R.id.NAMEOFTEXTVIEW);
            tv.setTextColor(Color.GREEN);
        }
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.achievement_item, parent, false);
        return row;
    }
}

my question is how do i implement this so that it works when getAchievements() is called from within my activity??? what XML files do i have to add? do i have to add an xml file with a linearlayout with a listview inside of it, then a seperate xml file with a linearlayout and a textview inside of it? or do i put the textview in the same layout as the listview is in? do i replace my entire getAchievements() function with cursoradapter code?

if someone could please help me out id appreciate it a lot. i really have no clue how to implement this. i've tried reading some tutorials and they're just confusing me a lot. if i could get a full example or something to help me understand how to do this it would help me out a LOT.


Solution

  • Since I'm not able to reproduce your development environment this is all I could do to refactor your code:

    public class ShowAchievements extends ListActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            DatabaseHandler db = new DatabaseHandler(this);
            Cursor cursor = db.getAchievements(this);
            MyCursorAdapter cursorAdapter = new MyCursorAdapter(this, cursor);
    
            this.setListAdapter(cursorAdapter);
        } 
    
        private Cursor getAchievements(Context context) {
            String selectQuery = "SELECT * FROM achievements ORDER BY id asc";
    
            SQLiteDatabase db = this.getWritableDatabase();
            return db.rawQuery(selectQuery, null);
        }
    
        private class MyCursorAdapter extends CursorAdapter {
    
            public MyCursorAdapter(Context context, Cursor c) {
                super(context, c);
            }
    
            @Override
            public void bindView(View v, Context context, Cursor cursor) {
                if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes"){
                    TextView tv = (TextView) v.findViewById(R.id.NAMEOFTEXTVIEW);
                    tv.setTextColor(Color.GREEN);
                }
            }
    
            @Override
            public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
                LayoutInflater inflater = (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.