Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:23:05+00:00 2026-06-04T16:23:05+00:00

I have an sqlite table with the following structure: String CREATE_ACHIEVEMENTS_TABLE = CREATE TABLE

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:23:07+00:00Added an answer on June 4, 2026 at 4:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple SQLite table called message: sequence INTEGER PRIMARY KEY type TEXT
I have sqlite table CREATE TABLE IF NOT EXISTS [app_status]( [id] INTEGER PRIMARY KEY
I have a database/table in SQLITE using the following structure CREATE TABLE milestones (
I have the following SQLite query: CREATE TABLE Logs ( Id integer IDENTITY (1,
I have the following table of counters: CREATE TABLE cache ( key text PRIMARY
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
I have created an sqlite table using the following statement: CREATE TABLE IF NOT
I have the following table: T(ID primary key, A, B) I want to have
I have a SQLite table like the following: +-----------+-------+ | StartTime | Name |
I have a table FOO with the following definition: id INTEGER, type INTEGER, data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.