I want to click each child expandable list view.(Inside fragment)

Asked 2 years ago, Updated 2 years ago, 41 views

First of all, with the help of Kwon Taehwan (Thank you.I applied a click event, but I can't click differently yet, so I'm replying the question ㅠ<


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

        groups = new String[]{};

        children = new String[][]{
    };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_lineup, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv = (ExpandableListView) view.findViewById(R.id.expListView);


    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final LayoutInflater inf;
        private String[] groups;
        private String[][] children;

        public ExpandableListAdapter(String[] groups, String[][] children) {
            this.groups = groups;
            this.children = children;

        }

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

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

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

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

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

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

        @Override
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }


How should I code the part with toast to make each child click differently? I'd like to put a link to a different Internet address. Currently, the same toast is printed on all children's items. I ask for your help me!

fragment listview android

2022-09-21 17:29

1 Answers

You wrote the following model for handing over to the adapter.

groups = new String[]{"Sports", "Academics", "Services", "Religion", "Performance", "Broadcasts, Newspapers", "Farmers";
children = new String[][]{
                {"a","b","c","d"},
                {"a"",n"",d"",dd"",hahaha"},
                {"l"",m"",b"",s"},
                {"""",""},
                {"d","dd"},
                {"dssdg","sdfsd"},
                {"ssssss","sssssssssss"};

When you click on the list, put the Url you want to move into the children array. For example, it's like this.

children = new String[][]{
                {"http://hashcode.co.kr","http://www.naver.com","http://www.daum.net","http://www.google.com"},
                {"http://www.stackoveflow.com", "..."},
                ...
                ...

Then, you can import Url from the child array using groupPosition and childPosition within the OnClickListener. Test it using the following code.

holder.text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(children[groupPosition][childPosition]));
        startActivity(intent);
    }
});


2022-09-21 17:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.