如何在ExpandableListView中单击子项打开新的Activity?

时间:2017-09-13 07:46:39

标签: android android-arrayadapter expandablelistview expandablelistadapter

我的ExapandableListView存在问题,OnChildClick更准确。我创建了一个ExpandableListView,我想通过单击此列表中的子项来打开一个Activity。我试过写一个代码,但它没有用。当我点击我的孩子时没有任何反应。你能帮我么。我会非常感谢任何帮助。

我的活动代码:

     import android.app.Activity;  
     import android.app.ExpandableListActivity;  
     import android.content.Context;  
     import android.content.Intent;  
     import android.support.v7.app.AppCompatActivity;  
     import android.os.Bundle;  
     import android.support.v7.widget.RecyclerView;  
     import android.view.LayoutInflater;  
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.BaseExpandableListAdapter;
     import android.widget.CheckedTextView;
     import android.widget.ExpandableListView;
     import android.widget.TextView;
     import android.widget.Toast;

    import java.util.ArrayList;

    public class HondacrossActivity extends ExpandableListActivity {


    // Create ArrayList to hold parent Items and Child Items
    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create Expandable List and set it's properties
        ExpandableListView expandableList = getExpandableListView();
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        // Set the Items of Parent
        setGroupParents();
        // Set The Child Data
        setChildData();

        // Create the Adapter
        final AdapterHondacross adapter = new AdapterHondacross(parentItems,
                childItems);

        adapter.setInflater(
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                this);

        // Set the Adapter to expandableList
        expandableList.setAdapter(adapter);

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


                if(childPosition==0){
                    Intent intent = new Intent(HondacrossActivity.this,KawascrossActivity.class);
                    startActivity(intent);
                }
                else if (childPosition ==1){
                    Intent intent = new Intent(HondacrossActivity.this,KawascruiserActivity.class);
                    startActivity(intent);
                }
                else if (childPosition ==2){
                    Intent intent = new Intent(HondacrossActivity.this,KawasenduroActivity.class);
                    startActivity(intent);}
                else if (childPosition ==3){
                    Intent intent = new Intent(HondacrossActivity.this,KawasroadsterActivity.class);
                    startActivity(intent);}


                return true;
            }
        });

            }

    // method to add parent Items
    public void setGroupParents() {
        parentItems.add("Cross1");
        parentItems.add("Cross2");
        parentItems.add("Cross3");
        parentItems.add("Cross4");
    }

    // method to set child data of each parent
    public void setChildData() {

        // Add Child Items for Fruits
        ArrayList<String> child = new ArrayList<String>();
        child.add("Cross1/1");
        child.add("Cross1.2");
        child.add("Cross1.3");
        child.add("Cross1.4");

        childItems.add(child);

        // Add Child Items for Flowers
        child = new ArrayList<String>();
        child.add("Cross2.1");
        child.add("Cross2.2");
        child.add("Cross2.3");
        child.add("Cross2.4");

        childItems.add(child);

        // Add Child Items for Animals
        child = new ArrayList<String>();
        child.add("Cross3.1");
        child.add("Cross3.2");
        child.add("Cross3.3");
        child.add("Cross3.4");

        childItems.add(child);

        // Add Child Items for Birds
        child = new ArrayList<String>();
        child.add("Cross4.1");
        child.add("Cross4.2");
        child.add("Cross4.3");
        child.add("Cross4.4");

        childItems.add(child);
    }


}  

我的适配器代码:

import android.app.Activity;  
import android.app.ExpandableListActivity;  
import android.content.Intent;  
import android.graphics.Color;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.AdapterView;  
import android.widget.BaseExpandableListAdapter;  
import android.widget.CheckedTextView;  
import android.widget.ExpandableListView;  
import android.widget.TextView;  
import android.widget.Toast;  

import java.util.ArrayList;  


public class AdapterHondacross extends BaseExpandableListAdapter {  
    private Activity activity;  
private ArrayList<Object> childtems;  
private LayoutInflater inflater;  
private ArrayList<String> parentItems, child;  

// constructor
public AdapterHondacross(ArrayList<String> parents,
                           ArrayList<Object> childern) {
    this.parentItems = parents;
    this.childtems = childern;
}

public void setInflater(LayoutInflater inflater, Activity activity) {
    this.inflater = inflater;
    this.activity = activity;
}

@Override
public int getGroupCount() {
    return parentItems.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return ((ArrayList<String>) childtems.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.hcross_1, null);
    }

    ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);

    return convertView;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    child = (ArrayList<String>) childtems.get(groupPosition);

    TextView textView = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.hcross_2, null);
    }

    // get the textView reference and set the value
    textView = (TextView) convertView.findViewById(R.id.textViewChild);
    textView.setText(child.get(childPosition));

    // set the ClickListener to handle the click event on child item
    convertView.setBackgroundColor(Color.CYAN);


          return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}}

1 个答案:

答案 0 :(得分:1)

更改AdapterHondacross的构造函数

// constructor
public AdapterHondacross(ArrayList<String> parents,
                           ArrayList<Object> childern, Activity activity) {
    this.parentItems = parents;
    this.childtems = childern;
    this.activity = activity;
}

用AdapterHondacross方法替换getChildView ...我希望它能帮到你..

@Override
    public View getChildView(int groupPosition, final int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                child = (ArrayList<String>) childtems.get(groupPosition);

                TextView textView = null;

                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.hcross_2, null);
                }

                // get the textView reference and set the value
                textView = (TextView) convertView.findViewById(R.id.textViewChild);
                textView.setText(child.get(childPosition));

                // set the ClickListener to handle the click event on child item
                convertView.setBackgroundColor(Color.CYAN);

               convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(childPosition==0){
                                Intent intent = new Intent(activity,KawascrossActivity.class);
                                activity.startActivity(intent);
                            }
                            else if (childPosition ==1){
                                Intent intent = new Intent(activity,KawascruiserActivity.class);
                                activity.startActivity(intent);
                            }
                            else if (childPosition ==2){
                                Intent intent = new Intent(activity,KawasenduroActivity.class);
                                activity.startActivity(intent);}
                            else if (childPosition ==3){
                                Intent intent = new Intent(activity,KawasroadsterActivity.class);
                                activity.startActivity(intent);}


                        }
               });


            return convertView;
}        

在HondacrossActivity中替换此行:

// Create the Adapter
        final AdapterHondacross adapter = new AdapterHondacross(parentItems,
                childItems, this);