当可扩展listview项目/子项点击时,无法启动/打开活动,以及如何在此处实现Toast

时间:2013-08-05 22:48:12

标签: android android-layout android-intent android-listview android-emulator

我在一个片段中创建了可扩展的listview,它的工作正常。但是,我想在单击可扩展listview子项时启动一个活动。我已经找了好几个小时试图找到解决方案但我无法找到它。有人请提供此问题的代码。

import android.R.color;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

        // This is a listfragment class
        public class Categories extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v = inflater.inflate(R.layout.categories, null);
                ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.expandableListView1);
                elv.setAdapter(new SavedTabsListAdapter());
                return v;
            }

            public class SavedTabsListAdapter extends BaseExpandableListAdapter {

                private String[] groups = { "Men", "Women"};

                private String[][] children = {
                    { "Men's accessories", "Men's trousers"},
                    { "Dresses", "Women's trousers"}
                };

                @Override
                public int getGroupCount() {
                    return groups.length;
                }

                @Override
                public int getChildrenCount(int i) {
                    return children[i].length;
                }

                @Override
                public Object getGroup(int i) {
                    return groups[i];
                }

                @Override
                public Object getChild(int i, int i1) {
                    return children[i][i1];
                }

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

                @Override
                public long getChildId(int i, int i1) {
                    return i1;
                }

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

                @Override
                public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
                    TextView textView = new TextView(CategoriesFragment.this.getActivity());
                    textView.setText(getGroup(i).toString());
                    return textView;
                }

                @Override
                public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
                    TextView textView = new TextView(CategoriesFragment.this.getActivity());
                    textView.setText(getChild(i, i1).toString());
                    return textView;
                }

                @Override
                public boolean isChildSelectable(int i, int i1) {
                    return true;
                }

            }

        Intent k;

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            switch(childPosition){
            case 0:
                k = new Intent(getActivity().getApplicationContext(), Baby.class);
                break;
            case 1:
                k = new Intent(getActivity().getApplicationContext(), Baby1.class);
                break;
            }
            startActivity(k);
            return true;
        }

        }

1 个答案:

答案 0 :(得分:1)

点击子元素childposition函数中的OnChildClick是什么?

你设置ChildClickListener的地方?如果您没有设置,则在此代码ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.expandableListView1);之后写下此行elv.setOnChildClickListener("your_listener");

另外,对于最佳实践,您可以通过以下方式编写上述侦听器和OnChildClick函数:

首先删除OnChildClick和OnChildClickListener(如果有的话),然后将代码放在elv.setOnChildClickListener("your_listener");上方的位置 -

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {     
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                     switch(childPosition){
                        case 0:
                            k = new Intent(getActivity().getApplicationContext(), Baby.class);
                            break;
                        case 1:
                            k = new Intent(getActivity().getApplicationContext(), Baby1.class);
                            break;
                        }
                        startActivity(k);
                        return true;
                }           
});

如果您仍然遇到任何问题,请告诉我们