Android的,改变阵列适配器列表的背景是什么? [英] Android, changing background of list in array adapter?

查看:199
本文介绍了Android的,改变阵列适配器列表的背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有名单我与阵列适配器它充气。每一件事情是确定,我有我的结果在列表中。

In my application i have list which i inflate it with array adapter. every thing is OK and i have my result in the list.

我想改变列表(偶数行红色为例,奇数行绿)的背景颜色。

I like to change background color of list (even rows red for example and odd rows green).

这是我行的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout
        android:id="@+id/rllist"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="15dip"
        >
        <TextView
            android:id="@+id/outstanding_contractdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/outstanding_contractdate"
            android:paddingLeft="20dip"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractamount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textSize="15sp" />
    </RelativeLayout>   
</LinearLayout>

我已经写了下面的codeS:

I have written following codes:

static class ViewHolder {
    RelativeLayout rlList;
    TextView tvContDate;
    TextView tvContNo;
    TextView tvContAmount;
}

和在阵列适配器我有这样的:

and in Array Adapter I have this one:

public View getView(int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            View row = convertView;

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_outstanding, parent, false);
                //convertView = mInflater.inflate(R.layout.list_outstanding, parent, false);

                holder = new ViewHolder();
                holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);
                holder.tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate);
                holder.tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno); 
                holder.tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount);

                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag(); 
            }

                holder.tvContDate.setText(llData.get(position).split(",")[0].trim());
                holder.tvContNo.setText(llData.get(position).split(",")[1].trim());
                holder.tvContAmount.setText(llData.get(position).split(",")[2].trim());

                if(position%2 != 0)
                    holder.rlList.setBackgroundColor(0x00FF00);
                else
                    holder.rlList.setBackgroundColor(0XFF0000); 

            return(row);
        }

运行的应用程序,当我到达这个code后,出现错误。根据LogCat中它是空指针异常,并指出这条线在getView():

after running application when i reach to this code, an error occurs. according to LogCat it is "Null Pointer Exception" and points to this line in getView():

holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);

似乎一切正常,但我不知道我的问题是什么?!

it seems everything is OK, but I don't know where my problem is?!!!

===========

===========

我在上面行更改为:

holder.rlList = (RelativeLayout) row.findViewById(R.id.rllist);

现在我已经不适用我的结果,但背景是什么?!

and now i have my result but background did not applied?!!!

推荐答案

尝试使用

if(position%2 != 0)
      holder.rlList.setBackgroundColor(Color.parseColor("#00ff00"));
else
      holder.rlList.setBackgroundColor(Color.parseColor("#ff0000"));

这篇关于Android的,改变阵列适配器列表的背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆