如何在展开可扩展listview(accordion)子项目时启动活动

时间:2015-10-12 10:57:39

标签: android

我是android新手。我想在点击可扩展listview子项时启动一个活动。我已经找了几个小时试图找到解决方案,但我没有找到任何适当的解决方案。有人请提供此问题的代码。

      package com.demo.spry.acordion1;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends ExpandableListActivity implements
        OnChildClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView expandbleLis = getExpandableListView();
        expandbleLis.setDividerHeight(2);
        expandbleLis.setGroupIndicator(null);
        expandbleLis.setClickable(true);

        setGroupData();
        setChildGroupData();


adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
adapter.notifyDataSetChanged();
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


        Intent myIntent = new Intent(MainActivity.this, Main5Activity.class);
        MainActivity.this.startActivity(myIntent);

    return false;


    }
});
    public void setGroupData() {
        groupItem.add("Car Details");
        groupItem.add("Appointment Details");
       /* groupItem.add("Manufacturer");
        groupItem.add("Extras");*/
    }

    ArrayList<String> groupItem = new ArrayList<String>();
    ArrayList<Object> childItem = new ArrayList<Object>();

    public void setChildGroupData() {
        /**
         * Add Data For TecthNology
         */
        ArrayList<String> child = new ArrayList<String>();
        child.add("Java");
        child.add("Drupal");
        child.add(".Net Framework");
        child.add("PHP");
        childItem.add(child);

        /**
         * Add Data For Mobile
         */
        child = new ArrayList<String>();
        child.add("Android");
        child.add("Window Mobile");
        child.add("iPHone");
        child.add("Blackberry");
        childItem.add(child);

    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {
        Toast.makeText(MainActivity.this, "Clicked On Child",
                Toast.LENGTH_SHORT).show();
        return true;
    }
}

2 个答案:

答案 0 :(得分:1)

这是一个例子,但我不确定你是否理解

如果您的列表来自活动

  list.setOnItemClickListener(new AdapterView.onItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {

    Intent intent = new Intent(YourActivity.this, SecondActivity.class);
           startActivity(intent);
       } 
    });

如果您的列表来自片段,请使用此

list.setOnItemClickListener(new AdapterView.onItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Intent intent = new Intent(getActivity(), SecondActivity.class);
       startActivity(intent);
   } 
});

答案 1 :(得分:0)

使用此:

  ExpandableListView.setOnChildClickListener(new  ExpandableListView.OnChildClickListener() {

                            @Override
                            public boolean onChildClick(ExpandableListView arg0,
                                    View arg1, int arg2, int arg3, long arg4) {
                              Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
                            CurrentActivity.this.startActivity(myIntent);
                                return false;
                            }
                        });