3

我的应用程序有问题:我使用带有个性化适配器的列表视图,在这个适配器中,我想根据消息是否被读取来更改行的颜色。

在方法 GETVIEW 我控制一个变量,如果它等于 0 我想改变背景颜色。

所有作品和列表按我的意愿显示,

但是当有很多元素并且列表在任何方向滚动(从上到下,反之亦然)时,即使通过代码设置了另一种颜色,原始数据也会显示相同的颜色。

有没有人遇到过同样的问题?你能给我一些建议吗?

有我的适配器的代码:

public class LazyAdapterComunicazioni extends BaseAdapter {

    private Activity activity;
    private String[] id;
    private String[] titolo;
    private String[] data;
    private String[] letto;
    private static LayoutInflater inflater=null;
    //public ImageLoader imageLoader; 

    public LazyAdapterComunicazioni(Activity a, String[] idCom, String[] titoloCom,      String[] dataCom, String[]lettoCom) {
        activity = a;
        id = idCom;
        titolo = titoloCom;
        data = dataCom;
        letto = lettoCom;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return id.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;

        if(convertView == null)
        {
            vi = inflater.inflate(R.layout.comunicazionicslist, null);
        }

        ContactsViewHolder viewHolder = new ContactsViewHolder();

        //Settimane
        viewHolder.txtTitolo=(TextView)vi.findViewById(R.id.comCS_Titolo);
        viewHolder.txtTitolo.setText(titolo[position].toString());

        //Data
        viewHolder.txtData=(TextView)vi.findViewById(R.id.comCS_Data);
        viewHolder.txtData.setText(data[position].toString());

        //ID
        viewHolder.txtID=(TextView)vi.findViewById(R.id.comCS_ID);
        viewHolder.txtID.setText(id[position].toString());

        //Connessianne e Apretura del DB
        String read = letto[position].toString();

        if (read.equals("0")) //DA LEGGERE
        {
            //LAYOUT
            viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA); 
            viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
            viewHolder.txtTitolo.setTextColor(Color.WHITE);
        }   

        return vi;
    }

    static class ContactsViewHolder {
        TextView txtTitolo;
        TextView txtData;
        TextView txtID;
        RelativeLayout rel;
    }
}

和单行的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/comCS_RIGA"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="#ffffff"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    >

    <TextView
        android:id="@+id/comCS_Data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000000"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:textSize="12sp"
        />

    <TextView
        android:id="@+id/comCS_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/comCS_Data"
        android:visibility="invisible"
        />

    <TextView
        android:id="@+id/comCS_Titolo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/comCS_Data"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="16dp"
        android:text=""
        android:textColor="#357cbc"
        android:textSize="18sp" 
        />

    <ImageView 
        android:id="@+id/feedbackCars_Positivo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/frecciacom"
        android:contentDescription="@string/title_Comunicazioni"
        android:layout_alignParentRight="true"
        android:layout_marginTop="2dp"
        android:layout_marginRight="16dp"
        />

</RelativeLayout>
4

2 回答 2

2

这是一个常见的错误。视图被回收,因此您必须在任何情况下设置回默认值。

    if (read.equals("0")) //DA LEGGERE
    {
        //LAYOUT
        viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA); 
        viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
        viewHolder.txtTitolo.setTextColor(Color.WHITE);
    }   
   else {
        viewHolder.rel.setBackgroundResource("you_defaulf_bg_res");
   }
于 2013-09-24T16:13:58.243 回答
1

首先,您没有正确使用 ViewHolder 模式。ViewHolder 旨在保存对列表项视图的引用并最小化调用findViewById(...)

您的代码无法实现该目的。您描述的行为的原因在于视图没有被回收。

像这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ContactsViewHolder viewHolder;

    if(convertView == null) {

        vi = inflater.inflate(R.layout.comunicazionicslist, null);
        viewHolder = new ContactsViewHolder();

        viewHolder.txtTitolo=(TextView)vi.findViewById(R.id.comCS_Titolo);
        viewHolder.txtData=(TextView)vi.findViewById(R.id.comCS_Data);
        viewHolder.txtID=(TextView)vi.findViewById(R.id.comCS_ID);

        viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA); 

        vi.setTag(viewHolder);

    } else {
        viewHolder = (ContactsViewHolder) vi.getTag();
    }

    //Settimane
    viewHolder.txtTitolo.setText(titolo[position].toString());

    //Data
    viewHolder.txtData.setText(data[position].toString());

    //ID
    viewHolder.txtID.setText(id[position].toString());

    //Connessianne e Apretura del DB
    String read = letto[position].toString();

    if (read.equals("0")) //DA LEGGERE {

        viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
        viewHolder.txtTitolo.setTextColor(Color.WHITE);
    } else {
        viewHolder.rel.setBackgroundResource(R.drawable.NORMAL_BACKGROUND);
    }

    return vi;
}
于 2013-09-24T16:17:27.400 回答