在可扩展列表视图中获取数据库行
Get database row in expandablelistview
我想从可扩展列表视图中的数据库中获取行号。我打算在 arraylist 中使用它来访问相关记录。除了查询游标和遍历所有记录之外,还有什么办法吗?这是我的代码:
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.i(TAG, "groupPos = " + groupPosition + " childPos = " + childPosition);
long packedPos = expandableListView.getExpandableListPosition(groupPosition);
Log.i(TAG, "packedPos = " + packedPos + "grouPos = "
+ ExpandableListView.getPackedPositionGroup(packedPos));
Log.i(TAG, " getPackedPositionForChild = " + ExpandableListView.
getPackedPositionForChild(groupPosition, childPosition));
Log.i(TAG, "getPackedPositionForChild= " + ExpandableListView.getPackedPositionChild
(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition)));
Log.i(TAG, "flatPos= " + expandableListView.getFlatListPosition
(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition)));
Log.i(TAG, "childId= " + mCursorAdapter.getChildId(groupPosition, childPosition));
Log.i(TAG, "combinedId= " + mCursorAdapter.getCombinedChildId(mCursorAdapter
.getGroupId(groupPosition), mCursorAdapter.getChildId(groupPosition, childPosition)));
Cursor c = mCursorAdapter.getChild(groupPosition, childPosition);
Log.i(TAG, "c.pos = " + c.getPosition());
Cursor g = mCursorAdapter.getGroup(groupPosition);
Log.i(TAG, "g.pos = " + g.getPosition());
Log.i(TAG, "selPos = " + expandableListView.getSelectedPosition());
Log.i(TAG, "selItemPos = " + expandableListView.getSelectedItemPosition());
Log.i(TAG, "selId = " + expandableListView.getSelectedId());
Log.i(TAG, "selItemId = " + expandableListView.getSelectedItemId());
TextView dateTV = (TextView) v.findViewById(R.id.date_textview);
TextView bGMeasurementTV = (TextView) v.findViewById(R.id.measurement_textview);
TextView timeOfDayTV = (TextView) (mCursorAdapter.getGroupView(groupPosition,
true, null, null)).findViewById(R.id.date_text_group);
String date = dateTV.getText().toString();
Float bGMeasurement = Float.valueOf(bGMeasurementTV.getText().toString());
String timeOfDay = timeOfDayTV.getText().toString();
Uri idUri = Uri.parse(BgContract.CONTENT_URI + "/" + id);
Intent i = new Intent(BgExpandableListActivity.this, MainActivity.class);
i.putExtra(DATE, date);
i.putExtra(BGMEASUREMENT, bGMeasurement);
i.putExtra(TIMEOFDAY, timeOfDay);
i.putExtra(IDURI, idUri.toString());
i.putExtra(ID, id); //that's the row number I am looking for
//startActivity(i);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
答案由以下代码给出:
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// Get group and child position -- expandableListView is a reference to an
// ExpandableListView widget
int groupPos = ExpandableListView.getPackedPositionGroup(expandableListView
.getExpandableListPosition(position));
int childPos = ExpandableListView.getPackedPositionChild(expandableListView
.getExpandableListPosition(position));
long childId = mCursorAdapter.getChildId(groupPos, childPos);
//Do whatever you want here...
return true;
}
我想从可扩展列表视图中的数据库中获取行号。我打算在 arraylist 中使用它来访问相关记录。除了查询游标和遍历所有记录之外,还有什么办法吗?这是我的代码:
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.i(TAG, "groupPos = " + groupPosition + " childPos = " + childPosition);
long packedPos = expandableListView.getExpandableListPosition(groupPosition);
Log.i(TAG, "packedPos = " + packedPos + "grouPos = "
+ ExpandableListView.getPackedPositionGroup(packedPos));
Log.i(TAG, " getPackedPositionForChild = " + ExpandableListView.
getPackedPositionForChild(groupPosition, childPosition));
Log.i(TAG, "getPackedPositionForChild= " + ExpandableListView.getPackedPositionChild
(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition)));
Log.i(TAG, "flatPos= " + expandableListView.getFlatListPosition
(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition)));
Log.i(TAG, "childId= " + mCursorAdapter.getChildId(groupPosition, childPosition));
Log.i(TAG, "combinedId= " + mCursorAdapter.getCombinedChildId(mCursorAdapter
.getGroupId(groupPosition), mCursorAdapter.getChildId(groupPosition, childPosition)));
Cursor c = mCursorAdapter.getChild(groupPosition, childPosition);
Log.i(TAG, "c.pos = " + c.getPosition());
Cursor g = mCursorAdapter.getGroup(groupPosition);
Log.i(TAG, "g.pos = " + g.getPosition());
Log.i(TAG, "selPos = " + expandableListView.getSelectedPosition());
Log.i(TAG, "selItemPos = " + expandableListView.getSelectedItemPosition());
Log.i(TAG, "selId = " + expandableListView.getSelectedId());
Log.i(TAG, "selItemId = " + expandableListView.getSelectedItemId());
TextView dateTV = (TextView) v.findViewById(R.id.date_textview);
TextView bGMeasurementTV = (TextView) v.findViewById(R.id.measurement_textview);
TextView timeOfDayTV = (TextView) (mCursorAdapter.getGroupView(groupPosition,
true, null, null)).findViewById(R.id.date_text_group);
String date = dateTV.getText().toString();
Float bGMeasurement = Float.valueOf(bGMeasurementTV.getText().toString());
String timeOfDay = timeOfDayTV.getText().toString();
Uri idUri = Uri.parse(BgContract.CONTENT_URI + "/" + id);
Intent i = new Intent(BgExpandableListActivity.this, MainActivity.class);
i.putExtra(DATE, date);
i.putExtra(BGMEASUREMENT, bGMeasurement);
i.putExtra(TIMEOFDAY, timeOfDay);
i.putExtra(IDURI, idUri.toString());
i.putExtra(ID, id); //that's the row number I am looking for
//startActivity(i);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
答案由以下代码给出:
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// Get group and child position -- expandableListView is a reference to an
// ExpandableListView widget
int groupPos = ExpandableListView.getPackedPositionGroup(expandableListView
.getExpandableListPosition(position));
int childPos = ExpandableListView.getPackedPositionChild(expandableListView
.getExpandableListPosition(position));
long childId = mCursorAdapter.getChildId(groupPos, childPos);
//Do whatever you want here...
return true;
}