我正在使用arraylist并在listview中显示值,现在我想要选定的文本。我试过但我无法完成它。这是我的代码,我希望将选定的文本存储在arraylist中。
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");
checkBoxData=new ArrayList<String>(Arrays.asList(myStrings));
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();
System.out.println("hi hi" +a);
}
});
}
}
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;
}
static class Holder {
CheckBox cbxSelected;
}
LayoutInflater inflater = null;
public CheckBoxListViewAdapter(Activity activity, ConferenceAdapter adapter, ArrayList<String> checkBoxData) {
this.calendars = checkBoxData;
selectedString = new ArrayList<String>();
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 row, ViewGroup parent) {
Context context = null;
Holder holder = null;
View vi = row;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(R.layout.ec_checkbox_number, null);
holder = new Holder();
holder.cbxSelected = (CheckBox)row.findViewById(R.id.checkBox);
row.setTag(holder);
}
else {
holder = (Holder)row.getTag();
holder.cbxSelected.setOnCheckedChangeListener(null);
}
holder.cbxSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked) {
selectedString.add("k");
}
else {
// do something
}
//cbxZoneChecked.setChecked(CurrentContext.RatingItems.Items.contains(ratingChoosenItems.get(position)));
}
});
return vi;
}
}
我知道如何完成这个。任何答案都会有所帮助
error is,10-23 13:18:42.900: E/AndroidRuntime(2901): FATAL EXCEPTION: main
10-23 13:18:42.900: E/AndroidRuntime(2901): java.lang.NullPointerException
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.LayoutInflater.from(LayoutInflater.java:211)
10-23 13:18:42.900: E/AndroidRuntime(2901): at com.bambeeq.conferencecall.CheckBoxListViewAdapter.getView(EcConferenceNumber.java:113)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.AbsListView.obtainView(AbsListView.java:2472)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.ListView.makeAndAddView(ListView.java:1775)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.ListView.fillDown(ListView.java:678)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.ListView.fillFromTop(ListView.java:739)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.ListView.layoutChildren(ListView.java:1628)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.AbsListView.onLayout(AbsListView.java:2307)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
10-23 13:18:42.900: E/AndroidRuntime(2901): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)