2012-10-18 180 views
0

我想取消選中按鈕單擊列表視圖中的所有複選框。 這是我的適配器類。它沒有選中所有複選框,但爲此我必須滾動瀏覽listview。 當我滾動它時,它根據可見的行取消選中。取消選中自定義列表視圖中的複選框

public class ExpenseCalculatorAdapter extends ArrayAdapter<String>{ 
    private Activity context; 
    private static LayoutInflater inflator = null; 
    private ArrayList<String> data; 
    private ArrayList<String> values; 
    DataBaseUtil dbUtils; 


    public ExpenseCalculatorAdapter(Activity context, ArrayList<String> data){ 
     super(context,R.layout.expenserow, data); 
     this.context = context; 
     this.data = data; 
     dbUtils=new DataBaseUtil(context); 


    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     String row=data.get(position); 
     String date; 
     String type; 
     String cost; 
     final String id; 
     /* result.add(c.getString(iRowid)+"&"+c.getString(iDate)+"~"+c.getString(iCost)+"#"+c.getString(iType)+"*");*/ 
     id=row.substring(0, row.indexOf("&")); 
     date=row.substring(row.indexOf("&")+1,row.indexOf("~")); 
     cost=row.substring(row.indexOf("~")+1,row.indexOf("#")); 
     type=row.substring(row.indexOf("#")+1,row.indexOf("*")); 

     if(convertView==null) 
     { 
      ViewHolder holder = new ViewHolder(); 
      inflator = context.getLayoutInflater(); 
      convertView = inflator.inflate(R.layout.expenserow, null); 

      holder.togCheck=(CheckBox)convertView.findViewById(R.id.check); 
      holder.textDate = (TextView) convertView.findViewById(R.id.txtrowDate); 
      holder.textType = (TextView) convertView.findViewById(R.id.txtrowType); 
      holder.textCost = (TextView) convertView.findViewById(R.id.txtrowCost); 

      holder.togCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        // TODO Auto-generated method stub 
        Intent intent=new Intent(); 
        intent.setAction("clicked"); 
        if(buttonView.isChecked()) 
        { 
         try 
         { 
          dbUtils.open(); 
          dbUtils.setVisibility(id); 
         }catch(SQLException sqx) 
         { 
          sqx.printStackTrace(); 
         } 
         catch(Exception ex) 
         { 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          dbUtils.close(); 
         } 
         intent.putExtra("isClicked","yes"); 
         intent.putExtra("ID",""+id); 
         context.sendBroadcast(intent); 
        } 
        else 
        { 
         try 
         { 
          dbUtils.open(); 
          dbUtils.setInVisibility(id); 
         }catch(SQLException sqx) 
         { 
          sqx.printStackTrace(); 
         } 
         catch(Exception ex) 
         { 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          dbUtils.close(); 
         } 
         intent.putExtra("isClicked","no"); 
         intent.putExtra("ID",""+id); 
         context.sendBroadcast(intent); 

        } 
       } 
      }); 




      convertView.setTag(holder); 

     } 

     ViewHolder hold = (ViewHolder) convertView.getTag(); 

     //setting Data to List 
     hold.textDate.setText(date); 
     hold.textType.setText(type); 
     hold.textCost.setText(cost); 



     if(CheckReceiver.checkAll) 
     { 
      hold.togCheck.setChecked(true); 


     } 
     if(!CheckReceiver.checkAll) 
     { 
      hold.togCheck.setChecked(false); 
     } 


     return convertView; 
    } 



    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return data.size(); 



    static class ViewHolder{ 

     public CheckBox togCheck; 
     public TextView textDate; 
     public TextView textType; 
     public TextView textCost; 

    } 

} 
+0

注意CheckReciver是BoradCastReciver我已經將checkAll變量設置爲true,如果我按下按鈕來檢查我的listview中的所有記錄。我做了這個原因,如果我使用adapter.getCount的條件簡單的循環。某些複選框尚未呈現。或者可能會被回收。所以它給了我Nullpointer例外。請幫我這個..它讓我瘋狂! –

回答

0

由於Listview回收視圖它不可能只用BroadCastReciver做到這一點。這真的是一個壞主意。 我使用Database和BroadCastReciver完成了這個任務。

holder.togCheck.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        /*Intent intent=new Intent(); 
        intent.setAction("clicked")*/; 
        if(holder.togCheck.isChecked()) 
        { 
         //holder.togCheck.setBackgroundResource(R.drawable.check); 
         try 
         { 
          dbUtils.open(); 
          dbUtils.setVisibility(id); 
         // Toast.makeText(context, "Checked ", Toast.LENGTH_SHORT).show(); 
         }catch(SQLException sqx) 
         { 
          sqx.printStackTrace(); 
         } 
         catch(Exception ex) 
         { 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          dbUtils.close(); 
         } 
         intent.putExtra("isClicked","yes"); 
         intent.putExtra("ID",""+id); 
         context.sendBroadcast(intent); 
        } 
        else 
        { 
        // holder.togCheck.setBackgroundResource(R.drawable.uncheck); 

         try 
         { 
          dbUtils.open(); 
          dbUtils.setInVisibility(id); 
          Toast.makeText(context, "UnChecked ", Toast.LENGTH_SHORT).show(); 
         }catch(SQLException sqx) 
         { 
          sqx.printStackTrace(); 
         } 
         catch(Exception ex) 
         { 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          dbUtils.close(); 
         } 
         intent.putExtra("isClicked","no"); 
         intent.putExtra("ID",""+id); 
         context.sendBroadcast(intent); 
        } 
       } 
      }); 

我做的是我根據數據庫中的id設置複選框的選中或未選中狀態。 然後通過提取狀態的id我做了計算!

相關問題