Reputation: 6857
I'd create the listview that contain checkbox and textview in each list. The problem which i'm facing is, I'm not able to retrieve the array of both checked and unchecked values from adapter class. I'd attached some sort of code here. Here is my Adapter class
public class MyFacilityAdapter<T> extends BaseAdapter {
ArrayList<T> mFList;
LayoutInflater mInflate;
SparseBooleanArray mySaprseBooleanArray;
ArrayList<Integer> items = new ArrayList<Integer>();
public MyFacilityAdapter(Context mContext, ArrayList<T> facilityList) {
// TODO Auto-generated constructor stub
mInflate = LayoutInflater.from(mContext);
mySaprseBooleanArray = new SparseBooleanArray();
mFList = new ArrayList<T>();
this.mFList = facilityList;
}
/*public ArrayList<T> getCheckeditems() {
ArrayList<T> tempArray = new ArrayList<T>();
for (int i = 0; i < mFList.size(); i++) {
if (mySaprseBooleanArray.get(i)) {
tempArray.add(mFList.get(i));
}
}
return tempArray;
}*/
ArrayList<Integer> getAdapterItems() {
return items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mFList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mFList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int postion, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflate.inflate(R.layout.row_facility, null);
}
TextView tvFName = (TextView) convertView
.findViewById(R.id.tv_facilityName);
tvFName.setText(mFList.get(postion).toString());
CheckBox chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
chkBox.setTag(postion);
/*chkBox.setChecked(mySaprseBooleanArray.get(postion));*/
chkBox.setOnCheckedChangeListener(mChkChangedListener);
return convertView;
}
OnCheckedChangeListener mChkChangedListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
CheckBox chk = (CheckBox) buttonView;
// TODO Auto-generated method stub
if (chk.isChecked()) {
items.set(chk.getTag(), 1);
} else {
items.set(chk.getTag(), 0);
}
/*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
}
};
}
I modified code, to [item.set(chk.getTag(), 1)] like that. but as soon as i checked the checkbox it fires an Indexoutofbounds exception. I attached here logcat.
04-02 01:21:53.002: E/AndroidRuntime(1019): FATAL EXCEPTION: main
04-02 01:21:53.002: E/AndroidRuntime(1019): Process: com.example.homeyapp, PID: 1019
04-02 01:21:53.002: E/AndroidRuntime(1019): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-02 01:21:53.002: E/AndroidRuntime(1019): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-02 01:21:53.002: E/AndroidRuntime(1019): at java.util.ArrayList.set(ArrayList.java:481)
04-02 01:21:53.002: E/AndroidRuntime(1019): at com.example.homeyapp.MyFacilityAdapter$1.onCheckedChanged(MyFacilityAdapter.java:95)
04-02 01:21:53.002: E/AndroidRuntime(1019): at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
04-02 01:21:53.002: E/AndroidRuntime(1019): at android.widget.CompoundButton.toggle(CompoundButton.java:87)
Upvotes: 0
Views: 1316
Reputation: 6857
Figured it out. First put the integer 1 or 0 into SparseBoolean array like:
if (chk.isChecked()) {
mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
} else {
mySaprseBooleanArray.put((Integer) chk.getTag(), isChecked);
}
then, In order to get checked and unchecked items array:
public ArrayList<Integer> getCheckeditems() {
ArrayList<Integer> items = new ArrayList<Integer>();
for (int i = 0; i < mFList.size(); i++) {
Boolean temp = mySaprseBooleanArray.get(i);
if (temp) {
items.add(1);
} else {
items.add(0);
}
}
return items;
}
here items return as a 100101010; 1 as checked and 0 as a unchecked.
Upvotes: 1
Reputation: 67044
The 'items' ArrayList needs to be pre-populated with the same number of elements as there are rows in your list. (Right now, you are appending a new integer element to the ArrayList every time the user checks or unchecks any row -- which makes no sense.)
If you do the above, then onCheckChanged() can look like:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox chk = (CheckBox) buttonView;
if (chk.isChecked()) {
items.set(chk.getTag(), 1);
} else {
items.set(chk.getTag(), 0);
}
/*mySaprseBooleanArray.put((Integer) buttonView.getTag(), isChecked);*/
}
Upvotes: 0