我有一个问题:我实现了一种从数据库中调用组和子项的方法,在将所有内容放入可扩展列表之后,直到那时,一切都很好,所有数据都按预期出来了。
但是,当我将带有单击子项的 Toast 消息调用到 onChildClick 方法中时,Toast 会显示组而不是子项(组也是错误的)谁能帮我找到我出错的地方?
记住:可扩展列表的输出是正确的
这是我的活动与可扩展列表:
public class ShowDeckActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carddeck);
try{
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(),
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.row_name },
createChildList(),
R.layout.child_row,
new String[] {"Sub Item"},
new int[] { R.id.grp_child}
);
setListAdapter( expListAdapter );
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
@SuppressWarnings("rawtypes")
private List createGroupList(){
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
return db.getGroupList();
}
@SuppressWarnings("rawtypes")
private List createChildList(){
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
return db.getChildList();
}
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
String nameLable = parent.getItemAtPosition(childPosition).toString();
// Debug
// System.out.println("Inside onChildClick at groupPosition = " + (groupPosition + 1) +" Child clicked at position " + (childPosition + 1));
Toast.makeText(getApplicationContext(), nameLable, Toast.LENGTH_SHORT).show();
return true;
}
}
这是我的 DatabaseHandler 方法:
public List getGroupList() {
String SQL = "SELECT * FROM assunto";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(SQL, null);
int numeroColunas = cursor.getCount();
ArrayList result = new ArrayList();
if(cursor.moveToFirst()){
do{
HashMap m = new HashMap();
m.put( "Group Item", cursor.getString(2) );
result.add( m );
}while(cursor.moveToNext());
}
cursor.close();
db.close();
return (List)result;
}
//@SuppressWarnings({ "unchecked", "rawtypes" })
public List getChildList(){
String sqlAssunto = "SELECT * FROM assunto";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursorAssunto = db.rawQuery(sqlAssunto, null);
ArrayList result = new ArrayList();
if(cursorAssunto.moveToFirst()){
do { // Loop begin 1
String assuntoID = cursorAssunto.getString(0);
String sqlDeck = "SELECT * FROM deck WHERE table_assuntos_id=" + assuntoID;
Cursor cursorDeck = db.rawQuery(sqlDeck, null);
ArrayList secList = new ArrayList();
if(cursorDeck.moveToFirst()){ // Move-se nos decks
do {
// Loop Begin 2
HashMap child = new HashMap();
child.put( "Sub Item", cursorDeck.getString(2) );
secList.add( child );
} while (cursorDeck.moveToNext()); // Loop end 2
}
result.add( secList );
} while (cursorAssunto.moveToNext()); // Loop end 1
}
cursorAssunto.close();
db.close();
return result;
}
可扩展列表输出(如预期):
GROUP1
- SUB ITEM1_1
- SUB ITEM1_2
- SUB ITEM1_3
GROUP2
- SUB ITEM2_1
- SUB ITEM2_2
GROUP3
- SUB ITEM3_1
GROUP4
[..]
示例:当我点击时(GROUP3 x SUB ITEM3_1)
错误的 Toast 消息是:
{Group Item= GROUP1}
...而不是预期的:
{Sub Item=SUB ITEM3_1}
有人知道发生了什么吗?提前致谢!
==================[已编辑,我找到了解决方案]======================== =
要解决此问题,您必须实例化一个 ExpandableListAdapter,如下所示:
ExpandableListAdapter adap = parent.getExpandableListAdapter();
String adaptar = adap.getChild(groupPosition, childPosition).toString(); // This get the correct child name
感谢帮助!