2012-04-07 26 views
9

Tôi đang cố gắng đặt OnCheckedChangeListener thành CheckBox nhưng ứng dụng của tôi thoát trong thời gian chạy. Tôi cũng đã cố gắng đặt người nghe cho số TextView của mình và tôi vẫn nhận được kết quả tương tự. Có ai giúp được không?Không thể đặt OnCheckedChangeListener thành Hộp kiểm

import android.app.ListActivity; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ListViewActivity extends ListActivity implements OnCheckedChangeListener { 

TextView label; 
CheckBox checkBox; 

public class MyCustomAdapter extends ArrayAdapter<String> { 


public MyCustomAdapter(Context context, int textViewResourceId, 
String[] objects) { 
super(context, textViewResourceId, objects); 

// TODO Auto-generated constructor stub 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
//return super.getView(position, convertView, parent); 

View row = convertView; 

if(row==null){ 
LayoutInflater inflater=getLayoutInflater(); 
row=inflater.inflate(R.layout.main, parent, false); 

} 


label=(TextView)row.findViewById(R.id.weekofday); 
label.setText(month[position]); 
checkBox=(CheckBox)row.findViewById(R.id.checkBox); 


return row; 
} 
} 

String[] month = { 
"January", "February", "March", "April", 
"May", "June", "July", "August", 
"September", "October", "November", "December" 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// setContentView(R.layout.main); 
/*setListAdapter(new ArrayAdapter<String>(this, 
    R.layout.row, R.id.weekofday, DayOfWeek));*/ 


    setListAdapter(new MyCustomAdapter(ListViewActivity.this, R.layout.main, month)); 

    checkBox.setOnCheckedChangeListener(this); //my application exits here!!!! 

} 





@Override 
public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
// TODO Auto-generated method stub 
//Toast.makeText(getApplicationContext(), "box checked", Toast.LENGTH_LONG); 

} 


} 

Trả lời

12

Bạn không thể đặt người nghe cho CheckBox của bạn từ ListView như vậy (có lẽ nó sẽ ném một NullPointerException), thay vì thiết lập người nghe trong phương pháp getView() (bạn cũng sẽ phải tiếp tục tình trạng CheckBox vì vậy bạn không kết thúc với tình trạng hàng kỳ lạ). Dưới đây là ví dụ:

public class ListViewActivity extends ListActivity { 

    public class MyCustomAdapter extends ArrayAdapter<String> { 

     private ArrayList<Boolean> status = new ArrayList<Boolean>(); 

     public MyCustomAdapter(Context context, int textViewResourceId, 
       String[] objects) { 
      super(context, textViewResourceId, objects); 
      for (int i = 0; i < objects.length; i++) { 
       status.add(false); 
      } 
     } 

     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      View row = convertView; 
      if (row == null) { 
       LayoutInflater inflater = getLayoutInflater(); 
       row = inflater.inflate(R.layout.adapters_listviewactivity_row, 
         parent, false); 
      } 

      TextView label = (TextView) row.findViewById(R.id.weekofday); 
      label.setText(month[position]); 
      CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox); 
      checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
        Toast.makeText(getApplicationContext(), "" + position, 
          Toast.LENGTH_SHORT).show(); 
        if (isChecked) { 
         status.set(position, true); 
        } else { 
         status.set(position, false); 
        } 
       } 
      }); 
      checkBox.setChecked(status.get(position)); 
      return row; 
     } 
    } 

    String[] month = { "January", "February", "March", "April", "May", "June", 
      "July", "August", "September", "October", "November", "December" }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new MyCustomAdapter(ListViewActivity.this, 
       R.layout.main, month)); 
    } 

} 

Đối với TextView bạn sẽ phải làm tương tự.

+0

Cảm ơn .... Đó là làm việc :) –

2

Thực hiện CheckBox Listener cho lớp học của bạn theo cách này, đặc biệt là nếu bạn có nhiều hơn một CheckBox để đối phó với, bạn có thể xử lý trong chuyển đổi trường hợp khối và làm gọn gàng mã của bạn:

public class MyClass extends AppCompatActivity implements 
    CompoundButton.OnCheckedChangeListener,{ 

     CheckBox myCheckBox; 
    } 

Trong của bạn phương pháp onCreate() đặt này:

myCheckBox = (CheckBox)findViewById(R.Id.myCheckBoxName_in_XML_layout); 
    mmyCheckBox.setOnCheckedChangeListener(this); 

Listern để xem sự kiện checkbox của bạn như thế này:

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

    switch (buttonView.getId()){ 
     case R.id.myCheckBoxName_in_XML_layout: 

      if(isChecked == true) { 
       Toast.makeText(this, "Checked", Toast.LENGTH_SHORT).show(); 
      } else{ 
        Toast.makeText(this, "Unchecked", Toast.LENGTH_SHORT).show(); 
        } 

      break; 
     } 

     } 
0
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { 
List<String> mList = new ArrayList<>(); 
CheckBox android, java, python, php, unity3D; 
Button submitButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    android = (CheckBox) findViewById(R.id.androidCheckBox); 
    android.setOnCheckedChangeListener(this); 
    java = (CheckBox) findViewById(R.id.javaCheckBox); 
    java.setOnCheckedChangeListener(this); 
    python = (CheckBox) findViewById(R.id.pythonCheckBox); 
    python.setOnCheckedChangeListener(this); 
    php = (CheckBox) findViewById(R.id.phpCheckBox); 
    php.setOnCheckedChangeListener(this); 
    unity3D = (CheckBox) findViewById(R.id.unityCheckBox); 
    unity3D.setOnCheckedChangeListener(this); 

    submitButton = (Button) findViewById(R.id.submitButton); 
    submitButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.e("ArrayList Values*******",mList.toString()); 
     } 
    }); 

} 

@Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
    switch (compoundButton.getId()) { 
     case R.id.androidCheckBox: 
      if (android.isChecked()) { 
       mList.add(String.valueOf(android.getText())); 
       Log.e("Android*******",mList.toString()); 
      } 
      else { 
        mList.remove(android.getText()); 
      } 
      break; 

     case R.id.javaCheckBox: 
      if (java.isChecked()) { 
       mList.add(String.valueOf(java.getText())); 
       Log.e("Java*******",mList.toString()); 
      } 
      else { 
       mList.remove(java.getText()); 
      } 
      break; 

     case R.id.phpCheckBox: 
      if (php.isChecked()) { 
       mList.add(String.valueOf(php.getText())); 
       Log.e("PHP*******",mList.toString()); 
      } 
      else { 
       mList.remove(php.getText()); 
      } 
      break; 

     case R.id.pythonCheckBox: 
      if (python.isChecked()){ 
       mList.add(String.valueOf(python.getText())); 
       Log.e("Python*******",mList.toString()); 
      } 
      else { 
       mList.remove(python.getText()); 
      } 
      break; 

     case R.id.unityCheckBox: 
      if (unity3D.isChecked()){ 
       mList.add(String.valueOf(unity3D.getText())); 
       Log.e("Unity*******",mList.toString()); 
      } 
      else { 
       mList.remove(unity3D.getText()); 
      } 
      break; 
     } 
    } 
} 
+0

Có, tôi đã làm mã –

Các vấn đề liên quan