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

时间:2015-12-24 05:09:57

标签: java android listview android-arrayadapter

我有一个列表视图,其中我有一些按钮。点击每个按钮我正在改变它的当前颜色&将其他颜色按钮设置为默认颜色。所以当我按下按钮清洁第一个位置时,它的颜色变为绿色&其他按钮颜色设置为灰色。我一次可见5行。当我向下滚动时,第7行的清洁按钮也变绿。我不知道为什么。我认为这是列表视图回收视图问题但是根据应该改变第6行的按钮颜色。请帮助我为什么会发生这种情况我已经尝试了所有可能使用的东西查看持有人模式但这也无法正常工作。

获取查看方法

@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;
                    }
                });

2 个答案:

答案 0 :(得分:0)

  
      
  1. 每次getView()时,都应该为按钮设置颜色,因为它可能是旧的重用按钮。因此,您的代码中的问题是您已将其设置为绿色但不是灰色。要解决这个问题,您只需要更改代码:
  2.   
 node --harmony nodeex/watcher.js  

到:

 const fs = require('fs');
 fs.watch(__dirname+'/target.txt', function() {
 console.log("File 'target.txt' just changed!");
 });
  console.log("Now watching target.txt for changes...");

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

  
      
  1. 但还有另一个问题:数据未更新。因此,您需要在最后一行之前更改onTouchListener中的onTouch():
  2.   
            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");
            }

运行良好。问题发生的原因是您已经更新了数据库中的数据,但没有更新Java Bean中的数据"值"。

答案 1 :(得分:0)

请将您的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;
}

让我知道更多问题。