自定义列表视图背景颜色在运行时更改

时间:2013-02-08 15:54:09

标签: android android-listview

我有一个带有自定义列表视图的应用程序,它具有textview和imageview。单击图像视图时,背景颜色应该更改。我尝试这样做,但获取NullPointerException ...这是我的代码

CustomListViewAdapter.java

public class ViewHolder {
    TextView mName;
    ImageView mDeleteImage;
}

public View getView(int position, View convertView, ViewGroup parent) {
    holder = null;
    DataFields rowItems = (DataFields) getItem(position);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);
        holder = new ViewHolder();
        holder.mName = (TextView) convertView.findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView)convertView.findViewById(R.id.hmFieldDeleteImage);

        convertView.setTag(holder);

        holder.mDeleteImage.setTag(position);

        holder.mDeleteImage.setOnClickListener(new ImageView.OnClickListener() {

            @Override
            public void onClick(final View view) {
                count++;
                mListRowRL = (RelativeLayout)view.findViewById(R.id.hmFieldMainRL);
                mListRowRL.setBackgroundColor(color.list_row_bg); //throwing NullPointerException
            }
        });
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());

    return convertView;
}

非常感谢任何建议。谢谢!

2 个答案:

答案 0 :(得分:0)

holder.mDeleteImage.setOnClickListener(new ImageView.OnClickListener() {

            @Override
            public void onClick(final View view) {
                count++;
                mListRowRL = (RelativeLayout)view.findViewById(R.id.hmFieldMainRL);
                mListRowRL.setBackgroundColor(color.list_row_bg); //throwing NullPointerException
            }
        });

您正在为ImageView分配一个点击监听器,这很好..但是在您的onClick方法中,您正在尝试搜索您的行传入的视图(哪个是ImageView).. ImageViews将不包含任何子视图你的mListRowRL将始终为null。 试试这样的事情

final View clickView = convertView;
holder.mDeleteImage.setOnClickListener(new ImageView.OnClickListener() {

                @Override
                public void onClick(final View view) {
                    count++;
                    clickView.setBackgroundColor(color.list_row_bg); 
                }
            });

答案 1 :(得分:0)

我曾经遇到过这个奇怪的错误

我用你的XML解决了它(好吧谷歌吧:)):

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />

然后,您将选择器分配给根行项目:

<LinearLayout
    android:id="@+id/LinearLayoutApplication"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/list_selector">

最后选择器(res / color上的list_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@color/light_blue"
        android:state_pressed="true" />
    <item
        android:drawable="@color/light_blue"
        android:state_selected="true" />
    <item
        android:drawable="@color/transparent" />
</selector>