我有一个问题: 在将所有数据放入expandablelist后,我实现了一个从数据库中调用组和子进程的方法,直到那时,一切都很好,所有数据都按预期发布。
但是当我将带有点击子项的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
感谢您的帮助!