Search code examples
androidsimplecursoradapter

Data from Cursor added to ListView with SimpleCursorAdapter shows white text (how to make it black)


Data from Cursor added to ListView with SimpleCursorAdapter shows white text (how to make it black) - see the image

enter image description here Here is the Simple cursor adapter code

public void displayWords(Cursor c){

    // Creates a new SimpleCursorAdapter
    SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
        getApplicationContext(),                    // The application's Context object
        android.R.layout.simple_list_item_1,        // A layout in XML for one row in the ListView
        c,                                          // The result from the query
        new String[] {DatabaseTable.COL_WORD},      // A string array of column names in the cursor
        new int[] { android.R.id.text1 });          // An integer array of view IDs in the row layout


    // Sets the adapter for the ListView
    setListAdapter(mCursorAdapter);

    /* Using SimpleCursorAdapter to get Data from DB.
     * stackoverflow.com/questions/12077955/android-using-simplecursoradapter-to-get-data-from-database-to-listview
     */
}

And the style resources used in AndroidManifes file

   <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 11 theme customizations can go here. -->
</style>

   <style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>

Solution

  • Like Egor said, if you could show some more code, that would be helpful.

    In the meantime: It looks like you are using list-view items that have a 'light' (holo) theme while your app (or just that activity() uses a 'dark' (holo) theme. The textviews' text-color is picked up from the app's dark theme (white font color) on top of white background.

    To figure out why that happens, we need more code (AndroidManifest.xml, for example) from you.

    Update after OP's comment:

    public void displayWords(Cursor c){
    
        // Creates a new SimpleCursorAdapter
        SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
            getApplicationContext(),                    // The application's Context object
            android.R.layout.simple_list_item_1,        // A layout in XML for one row in the ListView
            c,                                          // The result from the query
            new String[] {DatabaseTable.COL_WORD},      // A string array of column names in the cursor
            new int[] { android.R.id.text1 }){
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                View newView = super.newView(context, cursor, parent);
                ((TextView)newView.findViewById(android.R.id.text1)).setTextColor(Color.BLACK);
    return newView;
            }
        };          // An integer array of view IDs in the row layout
    
    
        // Sets the adapter for the ListView
        setListAdapter(mCursorAdapter);
    
        /* Using SimpleCursorAdapter to get Data from DB.
         * stackoverflow.com/questions/12077955/android-using-simplecursoradapter-to-get-data-from-database-to-listview
         */
      }
    

    I added an override of the adapter's newView method to your code, which would allow you to set/change the text's color. Try it and see if it works.