11

For some reason I cannot seem to update the color of the text in my ListView items. I'm not sure exactly why this is happening, does anyone have an idea?

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:textColor="@android:color/white"
        android:textSize="20sp">

    </ListView>

</RelativeLayout>

JAVA:

public class MainActivity extends Activity {
    Context context;
    ArrayList<String> aa = new ArrayList<String>();
        ListView lv;
        final String URL = "http://news.google.com";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);
            setContentView(R.layout.splash);
            lv= (ListView) findViewById(R.id.listView1);
            new MyTask().execute(URL);
        }

        private class MyTask extends AsyncTask<String, Void, String> {
            ProgressDialog prog;
            String title = "";

            @Override
            protected void onPreExecute() {
                prog = new ProgressDialog(MainActivity.this);
                prog.setMessage("Loading....");
                prog.show();
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    Document doc = Jsoup.connect(params[0]).get();
                    Element tableHeader = doc.select("tr").first();

                    for (Element element : tableHeader.children()) {
                        aa.add(element.text().toString());
                    }

                    title = doc.title();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                prog.dismiss();
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aa);
                lv.setAdapter(adapter);
            }
        }
    }

Edit (in response to Philipp Jahoda's answer):

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        prog.dismiss();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(adapter);
    }
}

Edit (in response to Philipp Jahoda's answer):

custom_textView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


</LinearLayout>

Logcat (in response to Philipp Jahoda's answer):

09-19 15:58:57.972: D/dalvikvm(2735): Late-enabling CheckJNI
09-19 15:58:58.063: E/Trace(2735): error opening trace file: No such file or directory (2)
09-19 15:58:58.713: D/libEGL(2735): loaded /system/lib/egl/libEGL_adreno200.so
09-19 15:58:58.723: D/libEGL(2735): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-19 15:58:58.723: D/libEGL(2735): loaded /system/lib/egl/libGLESv2_adreno200.so
09-19 15:58:58.723: I/Adreno200-EGL(2735): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
09-19 15:58:58.723: I/Adreno200-EGL(2735): Build Date: 07/31/12 Tue
09-19 15:58:58.723: I/Adreno200-EGL(2735): Local Branch: 
09-19 15:58:58.723: I/Adreno200-EGL(2735): Remote Branch: quic/master
09-19 15:58:58.723: I/Adreno200-EGL(2735): Local Patches: NONE
09-19 15:58:58.723: I/Adreno200-EGL(2735): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 +  NOTHING
09-19 15:58:58.773: D/OpenGLRenderer(2735): Enabling debug mode 0
09-19 15:59:00.785: D/dalvikvm(2735): GC_CONCURRENT freed 4650K, 55% free 5584K/12288K, paused 3ms+3ms, total 74ms
09-19 15:59:00.815: I/dalvikvm(2735): Total arena pages for JIT: 11
09-19 15:59:00.896: I/dalvikvm(2735): Total arena pages for JIT: 12
09-19 15:59:01.196: D/dalvikvm(2735): GC_CONCURRENT freed 1348K, 51% free 6075K/12288K, paused 2ms+6ms, total 32ms
09-19 15:59:01.486: D/dalvikvm(2735): GC_CONCURRENT freed 1360K, 47% free 6558K/12288K, paused 3ms+3ms, total 47ms
09-19 15:59:01.486: D/dalvikvm(2735): WAIT_FOR_CONCURRENT_GC blocked 28ms
09-19 15:59:01.787: D/dalvikvm(2735): GC_CONCURRENT freed 1283K, 43% free 7088K/12288K, paused 2ms+3ms, total 45ms
09-19 15:59:15.251: E/Trace(3080): error opening trace file: No such file or directory (2)
09-19 15:59:15.441: D/libEGL(3080): loaded /system/lib/egl/libEGL_adreno200.so
09-19 15:59:15.441: D/libEGL(3080): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-19 15:59:15.441: D/libEGL(3080): loaded /system/lib/egl/libGLESv2_adreno200.so
09-19 15:59:15.441: I/Adreno200-EGL(3080): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
09-19 15:59:15.441: I/Adreno200-EGL(3080): Build Date: 07/31/12 Tue
09-19 15:59:15.441: I/Adreno200-EGL(3080): Local Branch: 
09-19 15:59:15.441: I/Adreno200-EGL(3080): Remote Branch: quic/master
09-19 15:59:15.441: I/Adreno200-EGL(3080): Local Patches: NONE
09-19 15:59:15.441: I/Adreno200-EGL(3080): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 +  NOTHING
09-19 15:59:15.461: D/OpenGLRenderer(3080): Enabling debug mode 0
09-19 15:59:16.943: D/dalvikvm(3080): GC_CONCURRENT freed 4653K, 55% free 5572K/12288K, paused 3ms+4ms, total 26ms
09-19 15:59:17.013: I/dalvikvm(3080): Total arena pages for JIT: 11
09-19 15:59:17.143: I/dalvikvm(3080): Total arena pages for JIT: 12
09-19 15:59:17.223: D/dalvikvm(3080): GC_CONCURRENT freed 1359K, 51% free 6056K/12288K, paused 4ms+2ms, total 47ms
09-19 15:59:17.223: D/dalvikvm(3080): WAIT_FOR_CONCURRENT_GC blocked 25ms
09-19 15:59:17.493: D/dalvikvm(3080): GC_CONCURRENT freed 1345K, 47% free 6553K/12288K, paused 2ms+2ms, total 43ms
09-19 15:59:17.493: D/dalvikvm(3080): WAIT_FOR_CONCURRENT_GC blocked 19ms
09-19 15:59:17.734: D/dalvikvm(3080): GC_CONCURRENT freed 1277K, 43% free 7022K/12288K, paused 2ms+3ms, total 37ms
09-19 15:59:48.897: D/dalvikvm(3080): GC_CONCURRENT freed 4158K, 63% free 4592K/12288K, paused 3ms+2ms, total 33ms
09-19 15:59:48.897: D/dalvikvm(3080): WAIT_FOR_CONCURRENT_GC blocked 19ms
09-19 15:59:49.457: D/dalvikvm(3080): GC_FOR_ALLOC freed 912K, 60% free 4929K/12288K, paused 16ms, total 16ms
09-19 15:59:49.487: D/dalvikvm(3080): GC_CONCURRENT freed 1224K, 55% free 5547K/12288K, paused 2ms+2ms, total 22ms
09-19 15:59:49.487: D/dalvikvm(3080): WAIT_FOR_CONCURRENT_GC blocked 17ms
09-19 15:59:49.718: D/dalvikvm(3080): GC_CONCURRENT freed 1255K, 51% free 6048K/12288K, paused 2ms+7ms, total 33ms
09-19 15:59:49.978: D/dalvikvm(3080): GC_CONCURRENT freed 1366K, 47% free 6525K/12288K, paused 3ms+2ms, total 44ms
09-19 15:59:49.978: D/dalvikvm(3080): WAIT_FOR_CONCURRENT_GC blocked 28ms
09-19 15:59:50.208: D/dalvikvm(3080): GC_CONCURRENT freed 1285K, 44% free 7000K/12288K, paused 4ms+4ms, total 39ms
09-19 15:59:50.458: D/dalvikvm(3080): GC_CONCURRENT freed 4326K, 65% free 4401K/12288K, paused 2ms+2ms, total 34ms
09-19 16:08:02.193: E/Trace(4285): error opening trace file: No such file or directory (2)
09-19 16:08:02.604: D/libEGL(4285): loaded /system/lib/egl/libEGL_adreno200.so
09-19 16:08:02.604: D/libEGL(4285): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-19 16:08:02.614: D/libEGL(4285): loaded /system/lib/egl/libGLESv2_adreno200.so
09-19 16:08:02.624: I/Adreno200-EGL(4285): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
09-19 16:08:02.624: I/Adreno200-EGL(4285): Build Date: 07/31/12 Tue
09-19 16:08:02.624: I/Adreno200-EGL(4285): Local Branch: 
09-19 16:08:02.624: I/Adreno200-EGL(4285): Remote Branch: quic/master
09-19 16:08:02.624: I/Adreno200-EGL(4285): Local Patches: NONE
09-19 16:08:02.624: I/Adreno200-EGL(4285): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 +  NOTHING
09-19 16:08:02.644: D/OpenGLRenderer(4285): Enabling debug mode 0
09-19 16:08:07.289: D/dalvikvm(4285): GC_CONCURRENT freed 4685K, 55% free 5576K/12288K, paused 2ms+1ms, total 22ms
09-19 16:08:07.359: I/dalvikvm(4285): Total arena pages for JIT: 11
09-19 16:08:07.359: I/dalvikvm(4285): Total arena pages for JIT: 12
09-19 16:08:07.629: D/dalvikvm(4285): GC_CONCURRENT freed 1345K, 51% free 6064K/12288K, paused 1ms+3ms, total 38ms
09-19 16:08:07.939: D/dalvikvm(4285): GC_CONCURRENT freed 1332K, 47% free 6550K/12288K, paused 2ms+2ms, total 33ms
09-19 16:08:08.220: D/dalvikvm(4285): GC_CONCURRENT freed 1350K, 43% free 7042K/12288K, paused 2ms+3ms, total 54ms
09-19 16:08:08.220: D/dalvikvm(4285): WAIT_FOR_CONCURRENT_GC blocked 28ms
09-19 16:08:08.530: E/ArrayAdapter(4285): You must supply a resource ID for a TextView
09-19 16:08:08.530: D/AndroidRuntime(4285): Shutting down VM
09-19 16:08:08.530: W/dalvikvm(4285): threadid=1: thread exiting with uncaught exception (group=0x41e13300)
09-19 16:08:08.540: E/AndroidRuntime(4285): FATAL EXCEPTION: main
09-19 16:08:08.540: E/AndroidRuntime(4285): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.AbsListView.obtainView(AbsListView.java:2273)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.ListView.onMeasure(ListView.java:1156)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.View.measure(View.java:15172)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.View.measure(View.java:15172)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.View.measure(View.java:15172)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.View.measure(View.java:15172)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2435)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.View.measure(View.java:15172)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.Choreographer.doFrame(Choreographer.java:525)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.os.Handler.handleCallback(Handler.java:615)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.os.Looper.loop(Looper.java:137)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.app.ActivityThread.main(ActivityThread.java:4931)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-19 16:08:08.540: E/AndroidRuntime(4285):     at dalvik.system.NativeStart.main(Native Method)
09-19 16:08:08.540: E/AndroidRuntime(4285): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
09-19 16:08:08.540: E/AndroidRuntime(4285):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
09-19 16:08:08.540: E/AndroidRuntime(4285):     ... 36 more
4

4 回答 4

20

You will need to create a custom layout for your ListView items where you set your desired textcolor.

In this way, you do not even need a custom-adapter.

e.g. custom_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"
    android:textColor="@color/white"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_height="fill_parent"/>

Then, you can use your layout with the ArrayAdapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
lv.setAdapter(adapter);
于 2013-09-19T19:59:33.043 回答
6

That's because android.R.layout.simple_list_item_1 use default text color, so the best way to do it is by making your own xml something like this:

< TextView
android:id="@android:id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="16sp"
/> 
于 2013-09-19T20:00:31.557 回答
0

I had the same task where I only need to change the text color to white (since I have a dark background image) but would like to keep the default "simple_list_item_1" layout.

Answers above messed the layout for me, so I searched for original layout's xml and just added android:textColor="@android:color/white" at the end:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textColor="@android:color/white" />

XML basis: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_1.xml

于 2016-11-04T08:07:24.553 回答
-5

Try android:textColor="#FFFFFF" instead.

于 2013-09-19T20:01:43.320 回答