Android 加载和滚动时Listview行的颜色混合

Android 加载和滚动时Listview行的颜色混合,android,android-listview,adapter,baseadapter,Android,Android Listview,Adapter,Baseadapter,我正在根据一个子行的文本值设置该行的背景色。但是,无论文本值如何,都会设置多个背景。上下滚动时情况会变得更糟。适配器的代码: package com.test.app; 导入java.util.ArrayList; 导入android.content.Context; 导入android.util.Log; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.

我正在根据一个子行的文本值设置该行的背景色。但是,无论文本值如何,都会设置多个背景。上下滚动时情况会变得更糟。适配器的代码:

package com.test.app;
导入java.util.ArrayList;
导入android.content.Context;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类MyBaseAdapter扩展了BaseAdapter{
私有LayoutInflater mInflater=null;
private ArrayList mItems=new ArrayList();
公共MyBaseAdapter(上下文、ArrayList项){
mItems=项目;
mInflater=LayoutInflater.from(上下文);
}
public void addItem(字符串[]它){
添加(它);
}
公共无效集合列表项(ArrayList亮起){
mItems=发光;
}
@凌驾
public int getCount(){
返回mItems.size();
}
@凌驾
公共对象getItem(int位置){
返回mItems.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
静态类视窗夹{
公共文本视图tv0、tv1;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图行视图=空;
持票人持票人;
if(convertView==null)
{
rowView=mInflater.inflate(R.layout.history_行,空);
}
其他的
{
rowView=convertView;
}
viewHolder=新的viewHolder();
viewHolder.tv0=(TextView)rowView.findViewById(R.id.textView0);
viewHolder.tv1=(TextView)rowView.findViewById(R.id.textView1);
rowView.setTag(viewHolder);
ViewHolder=(ViewHolder)rowView.getTag();
holder.tv0.setText(mItems.get(position)[0].toString());
holder.tv1.setText(mItems.get(position)[1].toString());
if(holder.tv1.getText()等于(“0”))
{
rowView.setBackgroundColor(0xAA7777);
//只有包含“0”的行才应着色,但它会为多个随机行着色。
}
返回行视图;
}
}

您可以尝试,设置为ListView:
android:cacheColorHint=“#00000000”
您可以尝试,设置为ListView:
android:cacheColorHint=“#00000000”
如果您只使用:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
}
这确实会使包含
0
的行具有该特定颜色,但当您上下滚动列表时,具有该颜色的特定行将被回收,并最终出现在不应该出现的地方。正确的方法是,如果行不包含
0
,则为行提供默认颜色,以便覆盖可能回收视图的坏颜色:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
} else {
   rowView.setBackgroundColor(/*Here the default color will be*/)
   // This row doesn't contain 0 so it must have the default color.
  // Because you could be dealing with a recycled view(that has the above color)
 // then we must revert the color to the default to be sure we end up with the correct color.
}
ViewHolder
模式的正确代码为:

    View rowView = convertView;
    ViewHolder viewHolder;

    if(rowView == null) {
        rowView = mInflater.inflate(R.layout.history_row, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();    
    }             
然后使用
viewHolder
对象设置行的数据。

如果只使用:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
}
这确实会使包含
0
的行具有该特定颜色,但当您上下滚动列表时,具有该颜色的特定行将被回收,并最终出现在不应该出现的地方。正确的方法是,如果行不包含
0
,则为行提供默认颜色,以便覆盖可能回收视图的坏颜色:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
} else {
   rowView.setBackgroundColor(/*Here the default color will be*/)
   // This row doesn't contain 0 so it must have the default color.
  // Because you could be dealing with a recycled view(that has the above color)
 // then we must revert the color to the default to be sure we end up with the correct color.
}
ViewHolder
模式的正确代码为:

    View rowView = convertView;
    ViewHolder viewHolder;

    if(rowView == null) {
        rowView = mInflater.inflate(R.layout.history_row, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();    
    }             

然后使用
viewHolder
对象设置行的数据。

这不起作用。经过一些搜索,我发现了一些类似的问题,但似乎无法在我自己的代码中实现它们:这不起作用。经过一番搜索,我发现了一些类似的问题,但似乎无法在我自己的代码中实现它们:做得好,解释得很好,完全符合我的要求。当我点击15次代表时,我会回来投票。现在唯一的问题是,一旦设置了
行视图的背景,向上或向下滚动会将其重置为原始颜色。编辑:我刚刚解决了这个问题:使用以下功能手动将tv0的文本设置为“0”即可
public void addSingleItem(int aLPos,int strPos,String it){mItems.get(aLPos)[strPos]=it;}
PERFECT!你让我的日子过得很开心!!非常感谢你!做得好,解释得好,正是我要求的。当我点击15次代表时,我会回来投票。现在唯一的问题是,一旦设置了
行视图的背景,向上或向下滚动会将其重置为原始颜色。编辑:我刚刚解决了这个问题:使用以下功能手动将tv0的文本设置为“0”即可
public void addSingleItem(int aLPos,int strPos,String it){mItems.get(aLPos)[strPos]=it;}
PERFECT!你让我的日子过得很开心!!非常感谢你!