【问题标题】:listview with custom adapter not working correctly on scrolling带有自定义适配器的列表视图在滚动时无法正常工作
【发布时间】:2017-05-29 13:33:52
【问题描述】:

我正在使用 customAdapter 在列表视图中显示 mydata。 它有一些条件检查一些文件,然后改变线性布局的背景颜色,或者改变一些文本视图的颜色。

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

3- 我在主要活动中使用此自定义适配器。没关系。但是当我为 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-

【问题讨论】:

  • 我认为 Override public Object getItem(int position) { return position; } 应该是 Override public Object getItem(int position) { return data.get(position); }
  • 不,没有区别!

标签: android listview custom-adapter


【解决方案1】:

你只实现了if case 为什么没有实现else case 发生这种情况是因为每次向上滚动时都会创建视图 下来所以定义 else 也用于检查和颜色并保持 checkbox 选中与否,您必须在 adapter 中创建 int Array 将他的选中状态保存在特定位置并将if else 条件检查取消选中您的复选框。

您的OnItemClick 无法正常工作,因为您在使用checkbox ListView 自定义行,所以有解决方案集 focusable false 在自定义行的xmllistview xml 中的true

希望对你有所帮助。

【讨论】:

  • 其他部分工作了 :) 但 OnItemClick 还没有工作!关于复选框,您的意思是在自定义适配器中创建一个 int 数组,当用户选中复选框时,我将 1 设置为它在数组中的位置?你可以给一些代码sn-ps吗?
  • int[] checkPos = new int[data.size()]; 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)); checkPos[position] = 1; } else if(b==false) { holder.layout1.setBackgroundColor(Color.TRANSPARENT); checkPos[position] = 0; } } });
  • if(checkPos[position]==1){ holder.checkBox.setChecked(true); } else{ holder.checkBox.setChecked(false); }
  • 在全局级别定义 int[] checkPos 并通过上述代码更新您的 getView 方法。
  • 如果 OnItemClick 在定义可聚焦后仍然无法工作,那么也分享您的 xml 文件。
猜你喜欢
  • 1970-01-01
  • 2014-03-01
  • 2015-04-24
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2017-08-29
相关资源
最近更新 更多