2014-09-23 83 views
0

我想動態更新(更改顏色和刪除線文本)當用戶檢查CheckBox時ListView內的TextView。Android使用複選框onClick刷新TextView?

我的代碼在某種程度上起作用,但由於某種原因,只有在複選框第二次點擊時,即用戶檢查沒有任何反應,用戶取消檢查並再次檢查TextView然後根據需要更新?

任何幫助表示讚賞。

我的代碼:

@Override 
public View getView(final int position, final View convertView, final ViewGroup parent) { 

    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.shoppinglist, parent, false); 
    } 

    String anItem = get_Item(position);//from item array 

    ((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem); 

    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL); 

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      View aView = convertView; 
      if (aView == null) { 
       aView = lInflater.inflate(R.layout.shoppinglist, parent, false); 
      } 
      if(isChecked){ 

       checked[position] = true; 
       ((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY); 
       ((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
         ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags() 
         | Paint.STRIKE_THRU_TEXT_FLAG); 
       //aView.invalidate(); 
       notifyDataSetChanged(); 

      } 
      else{ 
       checked[position] = false; 
      } 

     } 
    }); 

    checkBox.setChecked(checked[position]); 
    return view; 
} 

更新後的代碼如下建議:

@Override 
public View getView(final int position, final View convertView, final ViewGroup parent) { 

    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.shoppinglist, parent, false); 
    } 

    String anItem = get_Item(position);//from item array 

    ((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem); 

    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL); 

    checkBox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      View aView = convertView; 
      if (aView == null) { 
       aView = lInflater.inflate(R.layout.shoppinglist, parent, false); 
      } 
      if(((CheckBox) v).isChecked()) { 
       checked[position] = true; 
       ((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY); 
       ((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
         ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
       //aView.invalidate(); 
       notifyDataSetChanged(); 
      }else{ 
       checked[position] = false; 
      } 

     } 
    }); 

    checkBox.setChecked(checked[position]); 
    return view; 
} 

沒有改變問題的陳述。

回答

0

那麼我解決了它,不太清楚究竟發生了什麼,也許有人可以詳細解釋?無論如何,問題是我在getView()中重複聲明瞭TextView,只是聲明它爲一個變量(在這種情況下是電視),然後在該方法內調用電視。謝謝您的幫助。

 @Override 
    public View getView(final int position, final View convertView, final ViewGroup parent) { 

     View view = convertView; 

     if (view == null) { 
      view = lInflater.inflate(R.layout.shoppinglist, parent, false); 
     } 
     final TextView tv = (TextView)view.findViewById(R.id.tvSLItemName); 
     String anItem = get_Item(position);//from item array 
     tv.setText(anItem); 
     final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);   
     checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       View aView = convertView; 
       if (aView == null) { 
        aView = lInflater.inflate(R.layout.shoppinglist, parent, false); 
       } 
       if(isChecked){       
        checked[position] = true;      
        tv.setTextColor(Color.GRAY);      
        tv.setPaintFlags(
          ((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags() 
          | Paint.STRIKE_THRU_TEXT_FLAG); 
        notifyDataSetChanged();  
       } 
       else{ 
        checked[position] = false; 
       }     
      } 
     });   
     checkBox.setChecked(checked[position]); 
     return view; 
    }