在listview错误中单击行时设置背景行

时间:2014-11-02 13:33:03

标签: android listview background android-adapter

目前,这是我的代码。 我想创建一个列表视图,当在列表视图中单击行时,行的背景更改为RED。当行的颜色为红色时,单击行的颜色更改为白色。 但是运行应用程序,单击一行,然后滚动列表视图,不仅要点击行的颜色更改为RED,下面的一些行也会更改为RED。

适配器

public class ItemBaseAdapter extends BaseAdapter {

private String[] mArrayItems;
private Context context;

public ItemBaseAdapter(Context context, String[] arrays) {
    this.context = context;
    this.mArrayItems = arrays;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mArrayItems.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.item_layout, null);
        holder = new ViewHolder();
        holder.textViewItem = (TextView) convertView
                .findViewById(R.id.textViewItemList);
        holder.layoutItem = (LinearLayout) convertView
                .findViewById(R.id.layoutItemList);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.textViewItem.setText(mArrayItems[position]);

    holder.layoutItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (holder.isCheckLayout == false) {
                holder.isCheckLayout = true;
                holder.layoutItem.setBackgroundColor(Color.RED);
            } else {
                holder.isCheckLayout = false;
                holder.layoutItem.setBackgroundColor(Color.WHITE);
            }

        }
    });

    return convertView;
}

static class ViewHolder {
    TextView textViewItem;
    LinearLayout layoutItem;
    boolean isCheckLayout = false;
}

}

这是活动

公共类MainActivity扩展了Activity {

private ListView mListView;

String[] arrayList = { "a", "b", "c", "d", "e", "a", "b", "c", "d", "e",
        "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c",
        "d", "e", "a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "b",
        "c", "d", "e", "a", "b", "c", "d", "e", "d", "e", "a", "b", "c",
        "d", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b",
        "c", "d", "e" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.listViewItem);
    mListView.setAdapter(new ItemBaseAdapter(this, arrayList));
}

}

这是项目布局的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
    android:id="@+id/layoutItemList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/textViewItemList"
        android:layout_width="wrap_content"
        android:text="@string/action_settings"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
</LinearLayout>

我不知道,原因是什么?我该怎么办?

2 个答案:

答案 0 :(得分:1)

getView始终称为多次次(示例:滚动列表视图时),因此您必须执行if-else操作仅在单击按钮时。尝试将代码更改为:

holder.textViewItem.setText(mArrayItems[position]); 

// also add the if-else outside the onclick 
if (isChecked[position] == false) { 
holder.layoutItem.setBackgroundColor(Color.WHITE); 
} else { 
holder.layoutItem.setBackgroundColor(Color.RED); 
} 

holder.layoutItem.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
if (isChecked[position] == false) { 
isChecked[position] = true; 
} else { 
isChecked[position] = false; 
} 
notifyDataSetChanged(); // call the getView again 
} 
}); 

return convertView; 
} 

答案 1 :(得分:0)

我解决了我的问题。 这是我的代码适配器

public class ItemBaseAdapter extends BaseAdapter {

private String[] mArrayItems;
private Context context;
boolean[] isChecked;

public ItemBaseAdapter(Context context, String[] arrays) {
    this.context = context;
    this.mArrayItems = arrays;
    this.isChecked = new boolean[mArrayItems.length];
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mArrayItems.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.item_layout, null);
        holder = new ViewHolder();
        holder.textViewItem = (TextView) convertView
                .findViewById(R.id.textViewItemList);
        holder.layoutItem = (LinearLayout) convertView
                .findViewById(R.id.layoutItemList);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.textViewItem.setText(mArrayItems[position]);

    // also add the if-else outside the onclick
    if (isChecked[position] == false) {
        holder.layoutItem.setBackgroundColor(Color.WHITE);
    } else {
        holder.layoutItem.setBackgroundColor(Color.RED);
    }

    holder.layoutItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (isChecked[position] == false) {
                isChecked[position] = true;
            } else {
                isChecked[position] = false;
            }
            notifyDataSetChanged(); // call the getView again
        }
    });

    return convertView;
}

static class ViewHolder {
    TextView textViewItem;
    LinearLayout layoutItem;
}

}