2012-12-01 4 views
3

listView 안에 checkbox을 클릭하면 textViewPaint.STRIKE_THRU_TEXT_FLAG)으로 청구되는 문제가 있습니다. 사용자가 checkbox을 클릭하면 경고 라인이 textView에 표시됩니다.textView에 Paint.STRIKE_THRU_TEXT_FLAG를 설정하십시오

  public void bindView(View row, Context context, Cursor c) { 
      // TODO Auto-generated method stub 

      listName = (TextView) row.findViewById(R.id.produtName); 
      listCheck = (CheckBox) row.findViewById(R.id.check); 
          Item tag = (Item) listCheck.getTag(); 
      String pos = helper.getProductId(c); 
      Log.i(CN, "getView: no tag on " + pos); 
      tag = new Item(); 
      tag.id = Integer.parseInt(pos); 
      listCheck.setTag(tag); 
      listCheck.setChecked(false); 
         String status = helper.getProductStatusT(c); 
      if (Integer.parseInt(status) == 0) { 
       listCheck.setChecked(true); 
       listName.setPaintFlags(listName.getPaintFlags() 
         | Paint.STRIKE_THRU_TEXT_FLAG); 
      } else { 
       listCheck.setChecked(false); 
       listName.setPaintFlags(listName.getPaintFlags() 
         & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
      } 


      public View getView(int position, View convertView, ViewGroup parent) { 
      View tmpView = super.getView(position, convertView, parent); 
      Log.i(CN, "getView:" + position); 
      final CheckBox cBox = (CheckBox) tmpView.findViewById(R.id.check); 
      Item tag = (Item) cBox.getTag(); 

      cBox.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Item tag = (Item) v.getTag(); 
        if (tag == null) 
         Log.i(CN, "checkbox clicked no tag"); 

        else 

         helper.updateStatus(tag.id); 

        Log.i(CN, "checkbox clicked tag=" + tag.id); 

       } 
        if (cBox.isChecked()) { 
         Log.i(CN, " Checked!"); 
         // do some operations here 
        } else { 
         Log.i(CN, "NOT Checked!"); 
         // do some operations here 
         helper.updateStatus2(tag.id); 
        } 
       } 
      }); 
      return tmpView; 
     } 

checkBox가 정상적으로 작동합니다. 그러나 문제는 사용자가 체크 박스를 클릭하면 스트라이크 라인이 표시되지 않는다는 것입니다.

public class CustomCursorAdapter extends CursorAdapter { 

    private LayoutInflater inflator; 
    private boolean[] arrCb; 

    public CustomCursorAdapter(Context context, Cursor c, int flags) { 
     super(context, c, flags); 
     inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     arrCb = new boolean[c.getCount()]; 
     //^this will hold the checkbox states 
     resetArrcb(); 
    } 

    @Override 
    public View newView(Context context, Cursor c, ViewGroup parent) { 
     View v = inflator.inflate(R.layout.lv_row, parent, false); 
     // inflate the xml that the textView and checkbox is in 
     return v; 
    } 

    @Override 
    public void bindView(View row, Context context, final Cursor cursor) { 
     // you do everything else here. there's no need for getView when you use newView and bindView 
     final TextView listName = (TextView) row.findViewById(R.id.produtName); 
     final CheckBox listCheck = (CheckBox) row.findViewById(R.id.check); 

     if (arrCb[cursor.getPosition()]) { 
      listCheck.setChecked(true); 
      listName.setPaintFlags(listName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
     } else { 
      listCheck.setChecked(false); 
      listName.setPaintFlags(listName.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
     } 

     listCheck.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (listCheck.isChecked()) { 
        arrCb[cursor.getPosition()] = true; 
       } else { 
        arrCb[cursor.getPosition()] = false; 
       } 
       notifyDataSetChanged(); 
      } 
     }); 

    } 

    private void resetArrcb() { 
     // i'm using this to fill the collection but you could easily loop through your 
     // database to fill it with the correct values. 
     for (int i = 0; i < arrCb.length; i++) { 
      arrCb[i] = false; 
     } 
    } 

} 

을 그리고 난 잘 모르겠어요 : 사람은 내가 당신을 위해 노력하고 다음과 같은 경우에 나는 당신이 지금 시도 할 수있는 어떤 아이디어가있을 수 있습니다 생각

listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); 
+0

첫 번째로'newView '와 함께'bindView'를 사용하면 getView가 필요 없습니다. 'newView'를 사용하여 viewgroup xml을 부 풀린 다음,'bindView'에서 모든 코드를 수행하십시오. 그런데 왜 여기에 태그를 설정하고 가져 왔습니까? – mango

+0

전에 시도해 보았지만 listview의 리사이클 메커니즘. 사용자가 체크 박스를 클릭 한 다음 productID에 따라 데이터베이스에 업데이트 할 때 productID를 가져오고 싶습니다. – johnk

+0

사용자가 체크 박스를 클릭하면 productStatus가 데이터베이스로 업데이트됩니다. strike 라인을 표시하지 않았습니다 ... listName.setPaintFlags (listName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); public 내부에 넣어야합니다. View getView? – johnk

답변

1

는 참조 놓아야 위치를 알 , 그러나 당신이 선택한 것에 따라 당신의 데이터베이스를 조정하고 싶을 것 같습니다. 나는 모든 것이 끝났을 때, 당신의 활동이 종료 될 때 그것을하는 것이 좋습니다. 아마도 체크 박스 콜렉션을 반복 할 새로운 메소드를 설정 한 다음 데이터베이스에서 의도 한 것을 수행하십시오. 테이블 행과 1 : 1이 일치해야합니다. 바로 활동에 당신이 당신의 데이터베이스 작업을 할 수있는 대신 있도록

심지어 더 나은

편집, 당신의 배열을 반환하는 getter 메소드를합니다.

+0

okok ... 나는 그것을 시험해 본다. ..thx 정보를 – johnk

+0

방금 ​​내 게시물을 편집했습니다. 어쩌면 더 포괄적 일 수도 있습니다. – mango

+0

arrCb [cursor.getPosition(), 이것은 데이터베이스에서 위치를 가져 오는 것입니까? 나는이 부분을 이해하지 못한다 – johnk

관련 문제