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

断了今生、忘了曾经 提交于 2019-12-08 13:00:07

问题


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

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>

回答1:


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.




回答2:


The theme used by this is not the same theme used by the context returned from getApplicationContext(). The simple answer is always use this to maintain your theme.

The OP is using a Light theme, which this returns. getApplicationContext() does not contain an inflated theme. When it is inflated it includes the system default theme.

More detailed information can be found here




回答3:


Had the same problem recently. Used two ListActivities. Clicking an item on the first one started the second ListActivity. First one displayed the theme correctly, but the second one did the same as the screenshot shows in the OP.

Only difference was the context argument I used in my array adapter creation. First activity used the this reference and the second one I used the getApplicationContext (like the OP). Changing it to the this reference fixed the output.

Could someone with more knowledge maybe expand on why the getApplicationContext "theme" seems to differ from the theme accessed via this(ListActivity). I would have thought that as the theme is set in the application tag in the manifest this would be the theme used by all activities unless set otherwise. I admit my knowledge of the getContext... methods is limited and I could be interpreting this completely wrong.




回答4:


Try this :

TextView tv = (TextView)findViewById(android.R.id.text1);

tv.setTextColor(Color.BLACK);



来源:https://stackoverflow.com/questions/15070237/data-from-cursor-added-to-listview-with-simplecursoradapter-shows-white-text-ho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!