使用可扩展细节进行xml解析

时间:2012-08-10 09:54:41

标签: android xml-parsing expandablelistview

您好我有一个xml解析示例。我必须添加expandadable listview.how添加到这里。请帮助我。

这是我的代码:

public class SingleMenuItemActivity extends ExpandableListActivity {
static final String KEY_ARTIST = "payment_method";
static final String KEY_SUBTOTAL = "subtotal";
static final String KEY_DISCOUNT = "discount";

@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Intent in = getIntent();
         String subtotal = in.getStringExtra(KEY_SUBTOTAL);
         String discount = in.getStringExtra(KEY_DISCOUNT);
         String payment_method = in.getStringExtra(KEY_ARTIST);

         TextView lblSub = (TextView) findViewById(R.id.subtotal_label);
         TextView lblTotal = (TextView) findViewById(R.id.total_label);
         TextView lblPayment = (TextView) findViewById(R.id.payment_label);

         lblSub.setText(subtotal);
         lbldiscount.setText(discount);
         lblPayment.setText(payment_method);

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                createGroupList(),              // Creating group List.
                R.layout.group_row,             // Group item layout XML.
                new String[] { "Order Info" },  // the key of group item.
                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,             // Layout for sub-level entries(second level).
                new String[] {"payment_label:"},      // Keys in childData maps to display.
                new int[] { R.id.payment_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());
    }
}

/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < 1 ; ++i ) { // 15 groups........
        HashMap m = new HashMap();
        m.put( "Order Info","Order Info " + i ); // the key and it's value.
        result.add( m );
      }
      return (List)result;

}

/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 2 ; ++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();
        child.put( "payment_label", "payment_label " + n );

        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());
    }
}

} 如果我必须点击特定产品意味着它转到下一个活动。下一个活动有详细描述与可扩展按钮。如果我必须点击详细说明意味着它被扩展并显示payment_method,小计,折扣否则它留在更少。请帮助我。如何做....如何使用上述应用程序开发我的代码。

我的main.xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
  <ExpandableListView android:id="@+id/android:list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="@drawable/bkg"/>

 <TextView android:id="@+id/android:empty"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:text="No items"/>

这里我的输出是零...我可以在这做什么改变。请帮助我。

1 个答案:

答案 0 :(得分:0)

check the below code snippet:
ListView lv;
lv = (ListView) findViewById(R.id.ListView01);
lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
gotoActivity();

            }
        });

start your activity in gotoactivity method using startActivityforResult method to take the values