首页 文章

listview,自定义适配器在滚动时无法正常工作

提问于
浏览
0

我正在使用customAdapter在列表视图中显示mydata . 它有一些条件可以检查某些文件然后更改linearlayout的背景颜色,或者更改某些textview的颜色 .

每件事都是正确的,直到加载......问题如下:1 - 当我向下滚动列表并向上滚动时,所有项目的颜色都会改变! 2-当我勾选一个复选框并向下滚动时,其他一些行将自动检查!

3-我在main activity中使用这个自定义适配器 . 没关系 . 但是当我为listview设置listview.setOnItemClickListener时将无法正常工作!

我的代码出了什么问题?

以下是惯用类别代码:

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-

1 回答

  • 1

    你已经实现了只有在没有实现其他情况的情况下才发生的情况,因为每次向上和向下滚动时都会创建视图,因此定义其他也用于检查和颜色以及维护复选框是否已选中,您必须在其中创建一个int数组适配器将其检查状态保存在特定位置,并将if else条件设置为选中取消选中您的复选框 . 您的OnItemClick无法正常工作,因为您在ListView自定义行中使用了复选框,因此在自定义行的xml中的解决方案集可聚焦为false,在listview xml中为true . 希望它会对你有所帮助 .

相关问题