I know similar question asked before, but they don't solve my problem.
I am creating a check box expandable view whose data is coming from some Api. Now I want all selected check box value in my activity.
I am getting all values (I refer another question Expandable check box) and I got this value as return([Pair{4 4}, Pair{3 3}, Pair{0 0}, Pair{3 6}, Pair{1 5}, Pair{3 7}, Pair{4 3}, Pair{3 4}, Pair{1 6}, Pair{4 2}, Pair{3 5}]
). Now how could I get value from this value ?
private final Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>();
And
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_group_child, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.expandableChild);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.childCkBox);
// add tag to remember groupId/childId
final Pair<Long, Long> tag = new Pair<Long, Long>(
getGroupId(groupPosition),
getChildId(groupPosition, childPosition));
cb.setTag(tag);
// set checked if groupId/childId in checked items
cb.setChecked(mCheckedItems.contains(tag));
// set OnClickListener to handle checked switches
cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final Pair<Long, Long> tag = (Pair<Long, Long>) v.getTag();
if (cb.isChecked()) {
mCheckedItems.add(tag);
//mCheckBoxData.put(getGroupId(groupPosition),
getChildId(groupPosition, childPosition));
} else {
mCheckedItems.remove(tag);
//mCheckBoxData.remove(getGroupId(groupPosition),getChildId(groupPosition, childPosition));
}
}
});
txtListChild.setText(childText);
return convertView;
}
In Activity:
private Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>();
mCheckedItems = listAdapter.getCheckedItems();
System.out.println(mCheckedItems);
I know now how to retrieve data from this type.
I have multiple group and all data is coming through Api.
try this
create interface AdapterCallback.java
interface AdapterCallback {
void onMethodCallback(int pos, String name);
}
in your adapter
private AdapterCallback mAdapterCallback;
//constructor
public Adapter(Context context, ArrayList<DataClass> arrayList, AdapterCallback callback) {
this.context = context;
this.arrayList = arrayList;
this.mAdapterCallback = callback;
}
in your get view method call where you check/ uncheck checkbox
mAdapterCallback.onMethodCallback(rowItem.getPos(), rowItem.getName());
in your activity implement interface like this
public class MainActivity extends AppCompatActivity implements AdapterCallback
and override your interface method in to your activity like this
@Override
public void onMethodCallback(int pos, String name) {
Log.d("interface", "calling");
Log.d("pos", ""+pos);
Log.d("name", name);
}