2015-12-24 25 views
1

我有一个列表视图,其中我有一些按钮。单击每个按钮时,我正在改变它的当前颜色&设置其他颜色按钮作为默认颜色。所以当我按下按钮清理第一个位置,然后它的颜色变为绿色&其他按钮颜色设置为灰色。我一次可以看到5行。当我向下滚动时,第7行的干净按钮也会变绿。我不知道为什么。我认为这是列表视图回收视图问题,但根据第6行按钮的颜色应该改变。请帮助我为什么发生这种情况我已经尝试了我使用过的每一件可能的事情查看持有人模式但这也行不通。列表视图中滚动使用resued项目?

获取视图方法

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

您需要存储按钮的状态,即哪个按钮是'绿色',哪个按钮是'灰色'。 –

+0

我怎么能这样做。很少有人建议把按钮放在if块中,并且他们中的一些人建议把if块放在外面,那么什么是正确的方式? – Techiee

+0

根据位置状态,你正在改变'颜色',我是对吗? –

回答

0
  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. 但还有另一个问题:数据未被更新。所以,你需要改变onTouch()在你的onTouchListener,最后一行之前:
  return false; 

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

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

它运行良好。问题的发生是因为你已经用db更新了数据,但是没有更新你的Java Bean的“值”。

+0

我昨天试过,但不工作 – Techiee

+0

@deepakkumar你见过日志信息:“状态不是1”吗? –

+0

@deepakkumar你没有更新你的数据,所以它必须出错。看到我更新的答案。 –

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

让我知道了进一步的问题。

相关问题