每次在子列表中单击一个项目时打开新活动

时间:2015-07-02 09:02:32

标签: android expandablelistview

嗨,我真的很新。我有可扩展列表视图的代码。单击子项时,它只显示相同文本的Toast。我想点击任何子项目时打开一个新活动。任何帮助将不胜感激。

MyExpandableAdapter

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

	    // constructor
	    public MyExpandableAdapter(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;
	    }
	    
	    // method getChildView is called automatically for each child view.
	    //  Implement this method as per your requirement
	    @Override
	    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
	    {

	        child = (ArrayList<String>) childtems.get(groupPosition);

	        TextView textView = null;

	        if (convertView == null) {
	            convertView = inflater.inflate(R.layout.child_view, 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.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View view) {
	                Toast.makeText(activity, child.get(childPosition),
	                        Toast.LENGTH_SHORT).show();
	            }
	        });
	        return convertView;
	    }

	    // method getGroupView is called automatically for each parent item
	    // Implement this method as per your requirement
	    @Override
	    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
	    {

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

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

	        return convertView;
	    }

	    @Override
	    public Object getChild(int groupPosition, int childPosition) 
	    {
	        return null;
	    }

	    @Override
	    public long getChildId(int groupPosition, int childPosition) 
	    {
	        return 0;
	    }

	    @Override
	    public int getChildrenCount(int groupPosition) 
	    {
	        return ((ArrayList<String>) childtems.get(groupPosition)).size();
	    }

	    @Override
	    public Object getGroup(int groupPosition) 
	    {
	        return null;
	    }

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

	    @Override
	    public void onGroupCollapsed(int groupPosition) 
	    {
	        super.onGroupCollapsed(groupPosition);
	    }

	    @Override
	    public void onGroupExpanded(int groupPosition)
	    {
	        super.onGroupExpanded(groupPosition);
	    }

	    @Override
	    public long getGroupId(int groupPosition) 
	    {
	        return 0;
	    }

	    @Override
	    public boolean hasStableIds() 
	    {
	        return false;
	    }

	    @Override
	    public boolean isChildSelectable(int groupPosition, int childPosition)
	    {
	        return false;
	    }

}
Main Activity

public class MainActivity 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
        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);

        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        
        // Set the Adapter to expandableList
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    // method to add parent Items
    public void setGroupParents() 
    {
        parentItems.add("Company");
        parentItems.add("Pricing & Plans");
        parentItems.add("Services");
        parentItems.add("Careers");
        parentItems.add("Clients");
        parentItems.add("Contact Us");
    }

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

        // Add Child Items for Company
        ArrayList<String> child = new ArrayList<String>();
        child.add("Who We Are");
        child.add("Advantage @ KDIPL");
        child.add("Team");
       
        
        childItems.add(child);

        // Add Child Items for Pricing & Plans
        child = new ArrayList<String>();
        child.add("SEO Packages");
        child.add("SMO Packages");
        child.add("PPC Packages");
        child.add("Web Design Packages");
        child.add("Web Development Packages");
        child.add("Email Marketing Packages");
        child.add("Bulk SMS Packages");
        
        childItems.add(child);

        // Add Child Items for Services
        child = new ArrayList<String>();
       
        child.add("SMO Services");
        child.add("PPC Services");
        child.add("Web Design Services");
        child.add("Web Development Services");
        child.add("SEO Services");
        
        childItems.add(child);

        // Add Child Items for Careers
        child = new ArrayList<String>();
        child.add("Career with Us");
        child.add("Current Openings");
        child.add("Recruitment Process");
        
        childItems.add(child);
        
     // Add Child Items for Clients
        child = new ArrayList<String>();
        child.add("Testimonials");
        child.add("SEO Results");
        child.add("PPC Results");
        child.add("Portfolio");
        
        childItems.add(child);
        
     // Add Child Items for Contact Us
        child = new ArrayList<String>();
        child.add("Location");
        child.add("Request a Quote");
        child.add("Feedback");
        
        childItems.add(child);
        
        
    }
    
    
	
	
}

2 个答案:

答案 0 :(得分:0)

你可以写这个

convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                   Intent intent= new Intent(context, MainActivity.class);
                   context.startActivity(intent);
                }
            });

而不是

convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(activity, child.get(childPosition),
                            Toast.LENGTH_SHORT).show();
                }
            });

在您的适配器中创建一个构造函数

private Context context;
public MyExpandableAdapter (Context context) {
              this.context = context;     
         }

答案 1 :(得分:0)

在你的活动中添加这个

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

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    Toast.makeText(
                            getApplicationContext(),groupPosition+ " group "+childPosition+" is clicked", Toast.LENGTH_SHORT)
                            .show();

                    Intent in = new Intent(Class_Name.this, JUMP_Class.class);
                    startActivity(in);
                    return false;
                }
            });