Pour une raison quelconque Je n'arrive pas à mettre à jour la couleur du texte dans mes éléments ListView
. Je ne sais pas exactement pourquoi cela se produit, quelqu'un a-t-il une idée?
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 (en réponse à la réponse de Philipp Jahoda):
@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 (en réponse à la réponse de Philipp Jahoda):
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 (en réponse à la réponse de Philipp Jahoda):
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
Vous devrez créer une mise en page personnalisée} _ pour vos éléments ListView
dans lesquels vous définissez la couleur de texte souhaitée.
De cette façon, vous n'avez même pas besoin d'un adaptateur personnalisé.
par exemple. 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"/>
Ensuite, vous pouvez utiliser votre mise en page avec le ArrayAdapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
lv.setAdapter(adapter);
En effet, Android.R.layout.simple_list_item_1 utilise la couleur de texte par défaut. La meilleure façon de le faire est donc de créer votre propre fichier XML comme celui-ci:
< 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"
/>
J'ai eu la même tâche où je n'ai besoin que de changer la couleur du texte en blanc (car j'ai une image d'arrière-plan sombre), mais j'aimerais conserver la mise en page par défaut "simple_list_item_1".
Les réponses ci-dessus ont gâché la mise en page pour moi, j'ai donc cherché le xml de la mise en page originale et ajouté simplement Android:textColor="@Android:color/white"
à la fin:
<?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" />