2015-03-31 56 views
4

我有一個ListView其中checkbox顯示在每一行。
每次碰到checkbox時,我都會檢查它是否被檢查。
但每一次,第一項總是返回false。但是,如果選中第二項,則第一項複選框的.ischecked()方法總是返回true
這裏是我的代碼Android checkbox.isChecked()無法正常工作

public class CustomSpecificCar extends ArrayAdapter<JSONObject>{ 
    ListSpecificCars caller; 
    private JSONObject currentJson; 
    private CheckBox cbSelectedCar; 
    private int position; 

    public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){ 
     super(ctxt, R.layout.custom_specific_car_row, list); 
     this.caller = caller; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false); 
     this.position = position; 

     // Set the reference of the layout 
     currentJson      = getItem(this.position); 
     cbSelectedCar      = (CheckBox)customView.findViewById(R.id.cbSelectedCar); 
     TextView tvBrand     = (TextView)customView.findViewById(R.id.tvBrand); 
     TextView tvModel     = (TextView)customView.findViewById(R.id.tvModel); 
     TextView tvOwnerEditable   = (TextView)customView.findViewById(R.id.tvOwnerEditable); 
     TextView tvPriceEditable   = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable); 


     try { 
      tvBrand.setText(currentJson.getString("brand")); 
      tvModel.setText(currentJson.getString("model")); 
      tvOwnerEditable.setText(currentJson.getString("owner")); 
     } catch (JSONException e) { 
      Log.e(e.getClass().getName(), "JSONException", e); 
     } 

     cbSelectedCar.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
        if (cbSelectedCar.isChecked()){ 
         caller.updateClickedUsername(currentJson, true); // Add to the List 
         Log.d("Added click ", "ok"); 
        } 

        else if (!cbSelectedCar.isChecked()) { 
         caller.updateClickedUsername(currentJson, false); // Delete from the List 
         Log.d("Deleted click ", "nok"); 
        } 
     }}); 

     return customView; 
    } 

} 

我應該怎麼做來解決這個問題? 非常感謝提前!

回答

13

您應該使用setOnCheckedChangeListener。

樣品:

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 

     } 
     }); 

在您的代碼:

cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 
       if (isChecked){ 
        caller.updateClickedUsername(currentJson, true); // Add to the List 
        Log.d("Added click ", "ok"); 
       } 

       else if (!isChecked) { 
        caller.updateClickedUsername(currentJson, false); // Delete from the List 
        Log.d("Deleted click ", "nok"); 
       } 

     } 
     }); 
+0

@Emmanuel賽博爾德:非常感謝,工作就像魅力! – Mornor 2015-03-31 08:19:07

+0

歡迎您。不要忘記upvote答案,然後其他人可以看到這是一個「解決方案」 – 2015-03-31 08:19:37

+0

真棒兄弟! – 2017-07-08 21:21:39

-1

對於使用選中複選框作爲Onclicklistener這將幫助你

CheckBox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (((CheckBox) v).isChecked()) { 
       Toast.makeText(this, "CheckBox is checked", Toast.LENGTH_SHORT).show(); 
       } else { 
       Toast.makeText(this, "CheckBox is not checked", Toast.LENGTH_SHORT).show(); 
      } 
    });