2014-09-23 98 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; 
    }