我有一个带有复选框的列表视图。我想从列表视图中获取所选复选框的所有文本。因为,我是 android 新手,我参考了一些使用视图持有者但我没有使用的链接。
public class EcConferenceNumber extends Activity{
ListView checkBoxNumberListView;
ConferenceAdapter adapter;
Button doneBtn,cancelBtn;
EditText profileName;
CheckBoxListViewAdapter cbAdapter;
static ArrayList<String> checkBoxData = null;
String[] myStrings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ec_number_selection);
adapter = new ConferenceAdapter(this);
checkBoxNumberListView = (ListView) findViewById(R.id.numberselectionlistView);
doneBtn=(Button) findViewById(R.id.donenumberbutton);
cancelBtn=(Button) findViewById(R.id.cancelnumberbutton);
Intent intent = getIntent();
myStrings = intent.getStringArrayExtra("strings");
/*ArrayList<String> got= cbAdapter.getSelectedString();
System.out.println(""+got);*/
checkBoxData=new ArrayList<String>(Arrays.asList(myStrings));
//checkBoxNumberListView.setAdapter(new CheckBoxListViewAdapter(EcConferenceNumber.this, adapter, checkBoxData));
//System.out.println(""+checkBoxData);
final CheckBoxListViewAdapter checkBoxListViewAdapter = new CheckBoxListViewAdapter(
EcConferenceNumber.this, adapter, checkBoxData);
checkBoxListViewAdapter.setSelectedString(new ArrayList<String>());
checkBoxNumberListView.setAdapter(checkBoxListViewAdapter);
doneBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ArrayList<String> a= checkBoxListViewAdapter.getSelectedString();
for(String b:a)
System.out.println("hi hi" +b);
}
});
}
}
class CheckBoxListViewAdapter extends BaseAdapter {
private List<String> calendars = null;
ArrayList<String> selectedString;
public ArrayList<String> getSelectedString() {
return selectedString;
}
public void setSelectedString(ArrayList<String> selectedString) {
this.selectedString = selectedString;
}
LayoutInflater inflater = null;
public CheckBoxListViewAdapter(Activity activity, ConferenceAdapter adapter, ArrayList<String> checkBoxData) {
this.calendars = checkBoxData;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return calendars.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Context c = null;
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.ec_checkbox_number, null);
TextView cbTV = (TextView) vi.findViewById(R.id.checkboxtextView);
final CheckBox checkBox = (CheckBox) vi.findViewById(R.id.checkBox);
cbTV.setText(calendars.get(position));
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedString.add(checkBox.getText().toString());
}else{
selectedString.remove(checkBox.getText().toString());
}
}
});
return vi;
}
}
选择复选框时出现错误。我想存储所有选定的复选框文本。
ec_number_selection.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/meetingprofilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Profile Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/meetingprofilename"
android:layout_alignBottom="@+id/meetingprofilename"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/meetingprofilename"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/cancelnumberbutton"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/editText2"
android:text="Cancel" />
<Button
android:id="@+id/donenumberbutton"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/cancelnumberbutton"
android:text="Done" />
<ListView
android:id="@+id/numberselectionlistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/cancelnumberbutton"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" >
</ListView>
</RelativeLayout>
ec_checkBox_number.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="" />
<TextView
android:id="@+id/checkboxtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox"
android:layout_alignBottom="@+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_marginLeft="107dp"
android:text="TextView" />
</RelativeLayout>
错误日志
10-22 12:03:27.950: E/AndroidRuntime(25520): FATAL EXCEPTION: main
10-22 12:03:27.950: E/AndroidRuntime(25520): java.lang.NullPointerException
10-22 12:03:27.950: E/AndroidRuntime(25520): at com.bambeeq.conferencecall.CheckBoxListViewAdapter$1.onCheckedChanged(EcConferenceNumber.java:109)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.widget.CompoundButton.setChecked(CompoundButton.java:137)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.widget.CompoundButton.toggle(CompoundButton.java:92)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.widget.CompoundButton.performClick(CompoundButton.java:104)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.view.View$PerformClick.run(View.java:17420)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.os.Handler.handleCallback(Handler.java:615)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.os.Looper.loop(Looper.java:137)
10-22 12:03:27.950: E/AndroidRuntime(25520): at android.app.ActivityThread.main(ActivityThread.java:4944)
10-22 12:03:27.950: E/AndroidRuntime(25520): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:03:27.950: E/AndroidRuntime(25520): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 12:03:27.950: E/AndroidRuntime(25520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-22 12:03:27.950: E/AndroidRuntime(25520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-22 12:03:27.950: E/AndroidRuntime(25520): at dalvik.system.NativeStart.main(Native Method)
我在这里得到了结构。任何答案都会有帮助