在android中使用OnClickListener进行expandablelistview

时间:2014-04-23 07:01:09

标签: android xml

我是android的新手,请帮我解决这个问题。 在可扩展列表视图中,单击每个子列表应打开新活动。

这是我的代码

package com.example.index;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;


public class IndexMainActivity 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.activity_index_main);


        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

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

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

    }

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Part 1");
        listDataHeader.add("Part 2");
        listDataHeader.add("Part 3");

        // Adding child data
        List<String> parta = new ArrayList<String>();
        parta.add("Sweet Hour Of Prayer");
        parta.add("Prayer and Royal Family");
        parta.add("The Holy Bible-King James Version");
        parta.add("William shakespeare - Scriptres about prayer");

        List<String> partb = new ArrayList<String>();
        partb.add("Samuel Rutherford/scriptures-Thankfulness");

        List<String> partc = new ArrayList<String>();
        partc.add("Matthew Henry/Scriptures on - Faith");
        partc.add("John Wesley/Scriptures on - Freedom");
        partc.add("Charles Simeon/Scriptures on -Protection");
        partc.add("Christmas Evans/Scriptures on - Guidance");

        listDataChild.put(listDataHeader.get(0), parta); // Header, Child data
        listDataChild.put(listDataHeader.get(1), partb);
        listDataChild.put(listDataHeader.get(2), partc);
    }

}
现在有三个清单,即a部分,b部分和部分c,现在通过点击部分a,我将通过点击第一个孩子清单获得四个孩子清单,即甜蜜的祷告新活动应该打开。

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

public class MyActivity extends Activity implements ExpandableListView.OnChildClickListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_mainactivity);
     mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
     ExpandableListAdapter adapter = new ExpandableListAdapter(this,
     mExpandableListView, mGroupCollection);
     mExpandableListView.setAdapter(adapter);
     mExpandableListView.setOnChildClickListener(this);
}

 @Override
 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
 {
        Toast.makeText(getApplicationContext(), "Go to Activity :: "+childPosition, Toast.LENGTH_LONG).show();
        return true;
 }
}

答案 1 :(得分:0)

onChildClickListener有一个ExpandableListView。检查此代码:

yourListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

                // Open activity using intent...

                return false;
        }
    });

希望它有所帮助!