我正在android中开发一个客户端-服务器项目。我需要从url解析xml并将其放在Expandablelistview中。当我试图根据来自服务器的数据将孩子放在每个组中时,就会出现问题。我的代码只将第一个子值放在第一组中,第二个子值放在第二组中。
我需要将“payment_method”和“total”放在第一组中,名字和姓氏放在第二组中(例如:Groupname:OrderInfo,customerInfo, Childname(OrderInfo):payment_method,total, Childname(CustomerInfo):firstname,lastname ).我的代码可以将 Childname(OrderInfo) 放在 OrderInfo 组中,但是我希望在单击 customerInfo 组时需要,名字,姓氏也显示 customerInfo 组。请帮助我怎么做。这是我的代码:
public class SingleMenuItemActivity extends ExpandableListActivity {
static final String KEY_ARTIST = "payment_method";
static final String KEY_FNAME = "firstname";
static final String KEY_LNAME = "lastname";
static final String KEY_DURATION = "total";
static final String OrderInfo = "OrderInfo";
static final String CustomerInfo = "CustomerInfo";
private static final String GroupID = "GroupID";
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.particular);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // Creating group List.
R.layout.group_row,
// Group item layout XML.
new String[] { "GroupID"}, // the key of group item.e
new int[] { R.id.order},
// ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.single_list_item,
// new String[] {"KEY_ARTIST"},
// new int[] { R.id.payment_label} // Keys in childData maps to display.
// Layout for sub-level entries(second level).
new String[] {"KEY_ARTIST","KEY_DURATION","KEY_FNAME","KEY_LNAME"},
new int[] { R.id.name_label,R.id.cost_label,R.id.firstname,R.id.lastname}// Keys in childData maps to display.
// new int[] { R.id.payment_label,R.id.total_label} // Data under the keys above go into these TextViews.
);
setListAdapter( expListAdapter ); // setting the adapter in the list.
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
private List createGroupList() {
Log.d(TAG, "Adding groups values");
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> statusMap = new HashMap<String, String>();
statusMap.put(GroupID, "OrderInfo");
list.add(statusMap);
HashMap<String, String> usersMap = new HashMap<String, String>();
usersMap.put(GroupID, "CustomerInfo");
list.add(usersMap);
Log.d(TAG, "Adding groups values successfull");
return list;
}
/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < 1 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
/* each group need each HashMap-Here for each group we have 3 subgroups */
ArrayList secList = new ArrayList();
for( int n = 0 ; n < 1 ; n++ ) {
HashMap child = new HashMap();
String s= getIntent().getStringExtra("payment_method");
String s1= getIntent().getStringExtra("total");
child.put( "KEY_ARTIST", s);
child.put( "KEY_DURATION", s1);
secList.add( child);
}
result.add( secList );
}
return result;
}
public void onContentChanged () {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand (int groupPosition) {
try{
System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
}catch(Exception e){
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
我的疑问是如何将第一个子值放置在第一组中,第二个子值放置在第二组中。第一个子值放置在第一组中已成功完成......但我不能放置第二个子值第二组。如果我必须单击第二组意味着它被强制关闭。请帮助我。怎么做。