我之前知道类似的问题,但他们无法解决我的问题。
我正在创建一个check box expandable view
,其数据来自某些Api。现在我想在我的活动中选中所有选中的复选框值。
我正在获取所有值(我引用另一个问题Expandable check box)并且我将此值作为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}]
)。现在我怎么能从这个值中获得价值?
private final Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>();
和
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;
}
在活动中 - &gt;
private Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>();
mCheckedItems = listAdapter.getCheckedItems();
System.out.println(mCheckedItems);
我现在知道如何从这种类型中检索数据。欢迎任何建议和编辑。
编辑:我有多个小组,所有数据都来自Api。
答案 0 :(得分:2)
试试这个
创建接口AdapterCallback.java
interface AdapterCallback {
void onMethodCallback(int pos, String name);
}
适配器中的
private AdapterCallback mAdapterCallback;
//constructor
public Adapter(Context context, ArrayList<DataClass> arrayList, AdapterCallback callback) {
this.context = context;
this.arrayList = arrayList;
this.mAdapterCallback = callback;
}
mAdapterCallback.onMethodCallback(rowItem.getPos(), rowItem.getName());
在你的活动实现界面中
public class MainActivity extends AppCompatActivity implements AdapterCallback
并将您的界面方法覆盖到您的活动中,如此
@Override
public void onMethodCallback(int pos, String name) {
Log.d("interface", "calling");
Log.d("pos", ""+pos);
Log.d("name", name);
}