简体   繁体   English

设置ListView项目的背景?

[英]Set background for ListView items?

So, I have this ListView , which I would like that each one of its items had a background (the same background for all of them). 因此,我有这个ListView ,我希望它的每个items都有一个背景(所有背景都相同)。 So far I've been trying to use this code: 到目前为止,我一直在尝试使用以下代码:

listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<CharSequence> lv = ArrayAdapter.createFromResource(this,
            R.array.countries_array, android.R.layout.simple_list_item_1);
listView.setAdapter(lv);

for (Integer i = 0; i < listView.getCount(); ++i) {
    View listItem = listView.getChildAt(i);
    listItem.setBackgroundResource(R.drawable.edittext_custom);
}

It's simple, I increment i and then use it to know the position in which it is and it will set that items background, and it repeats until all items have the same background. 很简单,我将i递增,然后使用它知道它的位置,它将设置该项目的背景,并重复进行直到所有项目具有相同的背景。 Sound easy. 听起来很简单。 Is there a better way to do this? 有一个更好的方法吗? If not, can someone please help figure it out why this simple code doesn't work? 如果没有,有人可以帮忙弄清楚为什么这个简单的代码不起作用吗? I'm pretty sure I'm missing something basic. 我很确定我缺少一些基本的知识。

Exception: 例外:

05-18 15:08:45.659  10157-10157/com.example.fabiocompany.goquiz W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41917da0)
05-18 15:08:45.679  10157-10157/com.example.fabiocompany.goquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.fabiocompany.goquiz, PID: 10157
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fabiocompany.goquiz/com.example.fabiocompany.goquiz.TopicosActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
            at android.app.ActivityThread.access$900(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.fabiocompany.goquiz.TopicosActivity.onCreate(TopicosActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
            at android.app.ActivityThread.access$900(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

XML: XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/bggoquiz"
    tools:context="com.example.fabiocompany.goquiz.TopicosActivity"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/searchView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="20.0sp"
        />

    <SearchView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:background="@drawable/edittext_custom"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

If you need something else, please do tell, I'll try to help. 如果您还有其他需要,请告诉我,我将尽力提供帮助。

Instead of doing all this you could create a new xml file named listitem.xml with textview as below 除了执行所有这些操作,您还可以使用textview创建一个名为listitem.xml的新xml文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background = "<give your desired background>"
/>

Modify your ArrayAdapter to 修改您的ArrayAdapter为

ArrayAdapter<String> lv = new ArrayAdapter<String>(this,
            R.layout.listitem, R.id.tv_item, <your array>);

As you are applying same background to all items simply setting it ones in xml will do the work. 当您对所有项目应用相同的背景时,只需在xml中将其设置即可。 You do not require to loop through list. 您不需要遍历列表。

Problem is with your this method,. 问题是您的这种方法。 listView.getChildAt(i); this works only on the items which are currently visible So this is how you need to use them. 这仅适用于当前可见的项目,因此您需要使用它们。

final int numVisibleChildren = listView.getChildCount();
final int firstVisiblePosition = listView.getFirstVisiblePosition();
for (int i = firstVisiblePosition; i < firstVisiblePosition + numVisibleChildren; i++) {
     View view = listView.getChildAt(i - firstVisiblePosition);
     listItem.setBackgroundResource(R.drawable.edittext_custom);
}

Assuming that listView.getChildAt is returning null because the corresponding position has an invisible (or non-existent) object, then simply test for null ... 假设listView.getChildAt返回null因为相应的位置有一个不可见的(或不存在的)对象,那么只需测试null

for (Integer i = 0; i < listView.getCount(); ++i) {
    View listItem = listView.getChildAt(i);
    if (listItem != null) {
        listItem.setBackgroundResource(R.drawable.edittext_custom);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM