如何实现onchildclick监听器?

时间:2015-10-23 17:00:20

标签: android listview

所以我有一个可扩展的列表视图。我想要的是让每个孩子都可以点击。如果我按第一个我要打开class1,如果我按第二个我要打开class2,如果我按第三个我要打开class3等等...我是编程的新手请解释我,就像你会做一个假人一样。

我收到此错误消息

The constructor Intent(new ExpandableListView.OnChildClickListener(){}, Class<Men>) is undefined

源代码

package info.androidhive.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.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

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.activity_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);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            //Toast.makeText(getApplicationContext(),
                    //listDataHeader.get(groupPosition) + " Expanded",
                    //Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            //Toast.makeText(getApplicationContext(),
                    //listDataHeader.get(groupPosition) + " Collapsed",
                    //Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            //Toast.makeText(
                    //getApplicationContext(),
                //  listDataHeader.get(groupPosition)
                //          + " : "
                    //      + listDataChild.get(
                        //          listDataHeader.get(groupPosition)).get(
                        //          childPosition), Toast.LENGTH_SHORT)
                    //.show();


             switch( childPosition )
                {
                   case 0:  Intent Men = new Intent(this, Men.class);     
                            startActivity(Men);
                            break;
                   case 1:  Intent newActivity2 = new Intent(this, youtube.class);     
                            startActivity(newActivity2);
                            break;
                   case 2:  Intent newActivity1 = new Intent(this, olympiakos.class);     
                            startActivity(newActivity1);
                            break;
                   case 3:  Intent newActivity3 = new Intent(this, karaiskaki.class);     
                            startActivity(newActivity3);
                            break;
                   case 4:  Intent newActivity4= new Intent(this, reservetickets.class);     
                            startActivity(newActivity4);
                            break;
                }
            return false;
        }
    });
}

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

    // Adding child data
    listDataHeader.add("Top 250");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}

2 个答案:

答案 0 :(得分:0)

这基本上是因为当您在OnChildClickListener中引用this时,它引用的是OnChildClickListener,而不是Activity。请尝试使用MainActivity.this。或者您可以使用view.getContext()。两者都会给你正确的背景。

答案 1 :(得分:0)

您的申请中没有任何Men课程。这就是为什么它显示错误。请参阅正确的类名。为方便起见,请使用不同的意图名称

case 0:  Intent goactivity = new Intent(youractivity.this, men.class); 
startActivity(goactivity);

//as your coding looks looks like may be your class is **men** not **Men**

如果是男性再次检查是否延伸Activity