在列表视图中滚动使用的恢复项目



我有一个列表视图,其中有一些按钮。点击每一个按钮,我都会改变它的当前颜色;将其他按钮的颜色设置为默认颜色。所以当我在第一个位置按下"清洁"按钮时,它的颜色会变为绿色;其他按钮颜色设置为灰色。我一次可以看到5行。当我向下滚动时,第7行的"清除"按钮也变为绿色。我不知道为什么。我以为这是列表视图回收视图的问题,但根据第六排按钮的颜色应该改变。请帮助我为什么会发生这种情况,我已经尝试了所有可能的方法,我使用了View Holder Pattern,但也不起作用。

获取查看方法

@Override
    public View getView(final int position, View rowView, ViewGroup parent) {
        select_postion=position;
        Log.i("error","select--"+select_postion);
        note_pos = position;
        // View rowView = convertView;
         if (rowView == null) 
         {
                LayoutInflater inflater = context.getLayoutInflater();
                rowView = inflater.inflate(layoutResourceId, null, true);
                holder = new ViewHolder();
                holder.componentName = (TextView) rowView
                        .findViewById(R.id.location_list_row);
                holder.clean = (Button) rowView.findViewById(R.id.btn1);
                holder.dirty = (Button) rowView.findViewById(R.id.btn2);
                holder.dc = (Button) rowView.findViewById(R.id.btn3);
                holder.na = (Button) rowView.findViewById(R.id.btn4);
                holder.camra = (ImageView) rowView.findViewById(R.id.btn5);
                holder.camra.setTag(position);
                holder.notes = (Button) rowView.findViewById(R.id.btn6);
                holder.count_text = (TextView) rowView
                        .findViewById(R.id.circle_count);
                holder.red_circle = (ImageView) rowView.findViewById(R.id.img_red);
                holder.position=position;
                holder.clean.setTag(holder);
                holder.camra.setTag(holder);
                holder.na.setTag(holder);
                holder.dc.setTag(holder);
                holder.notes.setTag(holder);
                holder.dirty.setTag(holder);
                rowView.setTag(holder);
         }
         else 
         {      
             // rowView=convertView;
                holder=(ViewHolder)rowView.getTag();
         }
                LocationInspectionBean location_obj = values.get(position);
                if (values.get(position).getImages() != null)
                {
                    imgpath = values.get(position).getImages();
                    imgpath1 = imgpath.split(",");
                    count = imgpath1.length;
                    holder.red_circle.setVisibility(View.VISIBLE);
                    holder.count_text.setVisibility(View.VISIBLE);
                    holder.count_text.setText(String.valueOf(count));
                }
                holder.componentName.setText(location_obj.getComp_name());
                if (location_obj.getInspectionstatus().equalsIgnoreCase("1"))
                {
                    holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
                    Log.i("SYNC", "Status is 1");
                }
                else if (location_obj.getInspectionstatus().equalsIgnoreCase("3"))
                {
                    holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
                    Log.i("SYNC", "Status is 3");
                }
                else if (location_obj.getInspectionstatus().equalsIgnoreCase("4")) {
                    holder.na.setBackgroundColor(Color.parseColor("#0D6CC3"));
                    Log.i("SYNC", "Status is 4");
                }
                else if (location_obj.getInspectionstatus().equalsIgnoreCase("2")) {
                    holder.dc.setBackgroundColor(Color.parseColor("#E87403"));
                    Log.i("SYNC", "Status is 2");
                }
                if (location_obj.getNotes().isEmpty()) {
                    holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
                }
                else if (location_obj.getNotes().isEmpty()) {
                    holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
                }
                holder.clean.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        //  Log.i("SYNC", "camera"+String.valueOf(index));
                        v.setBackgroundColor(Color.parseColor("#1C6614"));
                        ViewHolder h = (ViewHolder)v.getTag();
                        index=  h.position;
                        Toast.makeText(getApplicationContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
                        h.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
                        h.na.setBackgroundColor(Color.parseColor("#8A8787"));
                        h.dc.setBackgroundColor(Color.parseColor("#8A8787"));
                        Status = "1";
                    //  index = (Integer)v.getTag();
                        Log.i("SYNC", String.valueOf(index));
                        String timeStamp = new SimpleDateFormat(
                                "MM/dd/yyyy hh:mm:ss a").format(new Date());
                        db.updateInspectionDetails(inspection_id, user_id,
                                location_inspection_array.get(position)
                                        .getComponentid(), subclient_id, client_id,
                                Status, images_path_string, timeStamp);
                        return false;
                    }
                });

请将您的getView()方法修改为低于

@Override
public View getView(final int position, View rowView, ViewGroup parent) {
    select_postion = position;
    Log.i("error", "select--" + select_postion);
    note_pos = position;
    // View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(layoutResourceId, null, true);
        holder = new ViewHolder();
        holder.componentName = (TextView) rowView
                .findViewById(R.id.location_list_row);
        holder.clean = (Button) rowView.findViewById(R.id.btn1);
        holder.dirty = (Button) rowView.findViewById(R.id.btn2);
        holder.dc = (Button) rowView.findViewById(R.id.btn3);
        holder.na = (Button) rowView.findViewById(R.id.btn4);
        holder.camra = (ImageView) rowView.findViewById(R.id.btn5);
        holder.camra.setTag(position);
        holder.notes = (Button) rowView.findViewById(R.id.btn6);
        holder.count_text = (TextView) rowView
                .findViewById(R.id.circle_count);
        holder.red_circle = (ImageView) rowView.findViewById(R.id.img_red);
        rowView.setTag(holder);
    } else {
        // rowView=convertView;
        holder = (ViewHolder) rowView.getTag();
    }
    holder.position = position;
    holder.clean.setTag(holder);
    holder.camra.setTag(holder);
    holder.na.setTag(holder);
    holder.dc.setTag(holder);
    holder.notes.setTag(holder);
    holder.dirty.setTag(holder);
    LocationInspectionBean location_obj = values.get(position);
    if (values.get(position).getImages() != null) {
        imgpath = values.get(position).getImages();
        imgpath1 = imgpath.split(",");
        count = imgpath1.length;
        holder.red_circle.setVisibility(View.VISIBLE);
        holder.count_text.setVisibility(View.VISIBLE);
        holder.count_text.setText(String.valueOf(count));
    }
    holder.componentName.setText(location_obj.getComp_name());
    holder.clean.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //  Log.i("SYNC", "camera"+String.valueOf(index));
            v.setBackgroundColor(Color.parseColor("#1C6614"));
            ViewHolder h = (ViewHolder) v.getTag();
            index = h.position;
            Toast.makeText(getApplicationContext(), Integer.toString(index), Toast.LENGTH_SHORT).show();
            h.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
            h.na.setBackgroundColor(Color.parseColor("#8A8787"));
            h.dc.setBackgroundColor(Color.parseColor("#8A8787"));
            Status = "1";
            //  index = (Integer)v.getTag();
            Log.i("onTouch", String.valueOf(index));
            String timeStamp = new SimpleDateFormat(
                    "MM/dd/yyyy hh:mm:ss a").format(new Date());
            values.get(index).setInspectionstatus("1");
            return false;
        }
    });
    if (location_obj.getInspectionstatus().equalsIgnoreCase("1")) {
        holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
        holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
        Log.i("SYNC", "Status is 1");
    } else if (location_obj.getInspectionstatus().equalsIgnoreCase("3")) {
        holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
        holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
        Log.i("SYNC", "Status is 3");
    } else if (location_obj.getInspectionstatus().equalsIgnoreCase("4")) {
        holder.na.setBackgroundColor(Color.parseColor("#0D6CC3"));
        holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
        Log.i("SYNC", "Status is 4");
    } else if (location_obj.getInspectionstatus().equalsIgnoreCase("2")) {
        holder.dc.setBackgroundColor(Color.parseColor("#E87403"));
        holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
        holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
        Log.i("SYNC", "Status is 2");
    }
    if (location_obj.getNotes().isEmpty()) {
        holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
    } else if (location_obj.getNotes().isEmpty()) {
        holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
    }
    return rowView;
}

请告诉我更多的问题。

  1. 每次getView()时,都应该为按钮设置颜色,因为它可能是旧的重复使用按钮。因此,代码中的问题是,您已将其设置为绿色,但没有设置为灰色。要解决此问题,只需更改代码:
            if (location_obj.getInspectionstatus().equalsIgnoreCase("1"))
            {
                holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
                Log.i("SYNC", "Status is 1");
            }

至:

            if (location_obj.getInspectionstatus().equalsIgnoreCase("1"))
            {
                holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
                Log.i("SYNC", "Status is 1");
            }
            else
            {
                holder.clean.setBackgroundColor(Color.parseColor({COLOR_GREY}));
                Log.i("SYNC", "Status is not 1");
            }

{COLOR_GREY}是您自定义的灰色字符串。

  1. 但还有另一个问题:数据没有更新。因此,您需要更改onTouchListener中的onTouch(),在最后一行之前:
            return false;

添加一行以更新数据,如:

            values.get(position).setInspectionstatus("1");
            return false;

它运行良好。问题的出现是因为您已经更新了数据库中的数据,但还没有更新Java Bean"值"中的数据。

最新更新