如何在expandablelistview中单击子项导航到下一个活动?

时间:2014-02-06 10:15:22

标签: android listview

我已经构建了一个可扩展的列表视图。当我点击子项目时,我想开始另一个活动。我怎么能实现这个目标?我尝试使用onChildClick方法,但没有工作。这是我的MainActivity类。请告诉我我该怎么做。

    package com.example.expandablelistview;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ExpandableListView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.ExpandableListView.OnChildClickListener;


    public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View view,
                int groupPosition, int childPosition, long id) {
            // selected item 
             String item = ((TextView) view).getText().toString();
             Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          startActivity(i);
       // sending data to new activity
          i.putExtra("item", item);
          startActivity(i);
          return false;

          }
      });
   }

  /*
 * Preparing the list data
 */
   private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Catalog");
    listDataHeader.add("My Account");
    listDataHeader.add("Library Info");

    // Adding child data
    List<String> catalog = new ArrayList<String>();
    catalog.add("Automobile");
    catalog.add("Computer Science");
    catalog.add("Civil");
    catalog.add("Electronics and Communication");
    catalog.add("Electrical and Electronics");
    catalog.add("Information Science");
    catalog.add("Industrial Production");
    catalog.add("Mechanical");
    catalog.add("Basic Sciences");

    List<String> myaccount = new ArrayList<String>();
    myaccount.add("Check Holds");
    myaccount.add("Unreserve Books");

    List<String> libraryinfo = new ArrayList<String>();
    libraryinfo.add("Library Hours");
    libraryinfo.add("Contact Library");

    listDataChild.put(listDataHeader.get(0), catalog); // Header, Child data
    listDataChild.put(listDataHeader.get(1), myaccount);
    listDataChild.put(listDataHeader.get(2), libraryinfo);
      }
    }

1 个答案:

答案 0 :(得分:1)

试试此代码

expListView.setOnChildClickListener(new OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View view,
            int groupPosition, int childPosition, long id) {
        // selected item 
         String item = ((TextView) view).getText().toString();
         Intent i = new Intent(getApplicationContext(), SingleListItem.class);
         i.putExtra("item", item);
         startActivity(i);
      }
      return false;
  });