带有自定义适配器的列表视图在滚动时无法正常工作

listview with custom adapter not working correctly on scrolling

我正在使用自定义适配器在列表视图中显示我的数据。 它有一些条件,检查一些文件,然后改变线性布局的背景颜色,或者改变一些文本视图的颜色。

在加载之前一切正常...问题如下: 1-在我向下滚动列表并向上滚动后,所有项目的颜色都会改变!!! 2- 当我选中一个复选框并向下滚动时,其他一些行将被自动选中!!!

3- 我在主 activity.it 中使用这个自定义适配器没问题。但是当我为 listview 设置 listview.setOnItemClickListener 时将不起作用!!!

我的代码有什么问题?

下面是自定义适配器 class 代码:

package ir.telad.houseagancy;
... 
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList data;
private LayoutInflater inflater;
int i=0;
/*************  CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,String re) {
       /********** Take passed values **********/
        activity = a;
        data=d;
        inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
}

@Override
public int getCount() {
    if(data.size()<=0)
        return 1;
    return data.size();
}

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

public class ViewHolder{
    public TextView name;
    public TextView moaref;
    public TextView address;
    public TextView tel;
    public TextView rahn;
    public TextView ejare;
    public TextView date;
    public ImageView image;
    public TextView foori;
    public LinearLayout layout1;
    public CheckBox checkBox;
    //public TableRow tblr;
}
@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View vi;
     final ViewHolder holder;
      ListModel tempValues;
     if(convertView==null){

         /****** Inflate list_item.xml file for each row ( Defined below ) *******/
         convertView = inflater.inflate(R.layout.list_item,null);
         /****** View Holder Object to contain tabitem.xml file elements ******/
         holder = new ViewHolder();
         holder.layout1=(LinearLayout)convertView.findViewById(R.id.layout1);
         holder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
         holder.name = (TextView) convertView.findViewById(R.id.list_txtName);
         holder.tel=(TextView)convertView.findViewById(R.id.list_txtTel);
         holder.address=(TextView)convertView.findViewById(R.id.list_txtAddress);
         holder.rahn=(TextView) convertView.findViewById(R.id.list_txtRahn);
         holder.ejare=(TextView) convertView.findViewById(R.id.list_txtEjare);
         holder.image=(ImageView) convertView.findViewById(R.id.list_image);
         holder.foori=(TextView) convertView.findViewById(R.id.list_txtFoori);
         holder.date=(TextView) convertView.findViewById(R.id.list_txtDate);
         vi=convertView;
        /************  Set holder with LayoutInflater ************/
         convertView.setTag(holder);

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


     if(data.size()<=0)
     {
         holder.name.setText("no data");
         holder.address.setText("");

     }
     else
     {
         /***** Get each Model object from Arraylist ********/
         tempValues = ( ListModel ) data.get( position );
         /************  Set Model values in Holder elements ***********/
          holder.name.setText( tempValues.Name );
          holder.tel.setText( tempValues.Phone );
          holder.address.setText( tempValues.Address );
          holder.rahn.setText( tempValues.Rahn );
          holder.ejare.setText( tempValues.Ejare );
          holder.date.setText(tempValues.Date);

         holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                 if(b==true)
                     holder.layout1.setBackgroundColor(Color.argb(150,250,100,100));
                 else if(b==false) holder.layout1.setBackgroundColor(Color.TRANSPARENT);
             }
         });
          if(tempValues.foori==1)
              holder.foori.setVisibility(View.VISIBLE);


          if(tempValues.state==3){
              //holder.tblr.setBackgroundColor(Color.parseColor("#00ff00"));
              holder.name.setTextColor(Color.parseColor("#3C7700"));
              holder.tel.setTextColor(Color.parseColor("#2C6700"));
              holder.rahn.setTextColor(Color.parseColor("#397249"));
              holder.ejare.setTextColor(Color.parseColor("#92CD00"));
          }

          if(tempValues.isAzad==1){
              //holder.tblr.setBackgroundColor(Color.parseColor("#00ff00"));
              holder.name.setTextColor(Color.parseColor("#0000FF"));
              holder.tel.setTextColor(Color.parseColor("#0000FF"));
              holder.rahn.setTextColor(Color.parseColor("#0000FF"));
              holder.ejare.setTextColor(Color.parseColor("#0000FF"));
          } 

     }
     return convertView;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
}

}

2-

You have implemented only if case why not implemented else case its happened because view is created every time when you scroll up and down so define else also for check and color and to maintain the checkbox checked or not you have to make a int Array in adapter to save his checked state at specific position and put the if else condition to check uncheck your checkbox.

your OnItemClick is not working because you are using checkbox in ListView Custom Row, so there is on solution set focusable false in xml of custom row and true in listview xml.

hope it will help you.