代码之家  ›  专栏  ›  技术社区  ›  ArloGrant Streets Of Boston

使用SimpleCursorAdapter添加到ListView的Cursor数据显示白色文本(如何使其变为黑色)

  •  0
  • ArloGrant Streets Of Boston  · 技术社区  · 12 年前

    使用SimpleCursorAdapter将Cursor中的数据添加到ListView中,显示白色文本(如何使其变为黑色)-请参阅图片

    enter image description here 这是简单光标适配器代码

    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
         */
    }
    

    以及AndroidManifes文件中使用的样式资源

       <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>
    
    4 回复  |  直到 12 年前
        1
  •  2
  •   ArloGrant Streets Of Boston    12 年前

    正如Egor所说,如果你能展示更多的代码,那将是有帮助的。

    在此期间: 看起来您使用的是具有“亮”(holo)主题的列表视图项,而您的应用程序(或仅该activity())使用的是“暗”(holo)主题。文本视图的文本颜色是从应用程序的白色背景上的深色主题(白色字体颜色)中提取的。

    为了弄清楚为什么会发生这种情况,我们需要您提供更多的代码(例如AndroidManifest.xml)。

    OP评论后更新:

    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
         */
      }
    

    我添加了适配器的覆盖 新建视图 方法,这将允许您设置/更改文本的颜色。试试看是否有效。

        2
  •  2
  •   CyberedElf    11 年前

    使用的主题 this 与从返回的上下文使用的主题不同 getApplicationContext() 。简单的答案是永远使用 以保持您的主题。

    OP使用了Light主题 返回。 获取应用程序上下文() 不包含夸大的主题。当它膨胀时,它包括系统默认主题。

    可以找到更详细的信息 here

        3
  •  1
  •   Gandalf171    12 年前

    最近也有同样的问题。使用了两个ListActivities。单击第一个项目上的一个项目会启动第二个ListActivity。第一个正确地显示了主题,但第二个与OP中显示的屏幕截图相同。

    唯一的区别是我在创建阵列适配器时使用的上下文参数。第一个活动使用this引用,第二个活动使用getApplicationContext(类似于OP)。将其更改为this引用固定了输出。

    有人能不能有更多的知识来解释为什么getApplicationContext“主题”似乎与通过此访问的主题不同(ListActivity)。我本以为,由于主题是在清单中的应用程序标记中设置的,除非另有设置,否则这将是所有活动使用的主题。我承认我知道getContext。。。方法是有限的,我可能会完全错误地解释这一点。

        4
  •  0
  •   h4ck3d    12 年前

    试试这个:

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

    tv.setTextColor(Color.BLACK);