마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

fragment에 expandableListview 실행중 오류

0 추천
**CustomExpandableListAdapter입니다
package com.example.user_page;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
    private final Context mContext;
    private final List<String> expandableListTitle;
    private final HashMap<String, List<String>> expandableListDetail;

    public CustomExpandableListAdapter(Context mContext, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
        this.mContext = mContext;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return this.expandableListDetail.get(this.expandableListTitle.get(i)).size();
    }

    @Override
    public Object getGroup(int i) {
        return this.expandableListTitle.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return this.expandableListDetail.get(this.expandableListTitle.get(i)).get(i1);
    }

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

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

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        String listTitle = (String) getGroup(i);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_header,null);
        }

        TextView listTitleTextView = view.findViewById(R.id.header_title);
        listTitleTextView.setText(listTitle);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        String expandedListText = (String) getChild(i,i1);
        if (view == null){
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_child,null);
        }
        TextView expandedListTextView = view.findViewById(R.id.child_title);
        expandedListTextView.setText(expandedListText);

        return view;
    }

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

 

**ExpandableListDataPump.java 입니다

package com.example.user_page;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ExpandableListDataPump {

    public static HashMap<String, List<String>> getData(){
        HashMap<String, List<String>> expandableListDetail = new HashMap<>();

        List<String> cricket = new ArrayList<>();
        cricket.add("a");
        cricket.add("b");
        cricket.add("c");
        cricket.add("d");
        cricket.add("f");

        List<String> football = new ArrayList<>();
        football.add("a");
        football.add("b");
        football.add("c");
        football.add("d");
        football.add("f");

        List<String> basketball = new ArrayList<>();
        basketball.add("a");
        basketball.add("b");
        basketball.add("c");
        basketball.add("d");
        basketball.add("f");


        expandableListDetail.put("CRICKET", cricket);
        expandableListDetail.put("FOOTBALL", football);
        expandableListDetail.put("BASKETBALL", basketball);

        return expandableListDetail;
    }


}

 

**프래그먼트입니다

package com.example.user_menu;




import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.user_page.CustomExpandableListAdapter;
import com.example.user_page.ExpandableListDataPump;
import com.example.user_page.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class user_1 extends Fragment {

    ExpandableListView expandableListView;
    ExpandableListAdapter expandableListAdapter;
    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail;

    ViewGroup viewGroup;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewGroup = (ViewGroup) inflater.inflate(R.layout.user_1, container, false);

        expandableListView = (viewGroup).findViewById(R.id.expandableListView);
        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList<>(expandableListDetail.keySet());

        expandableListAdapter = new CustomExpandableListAdapter(getActivity(),expandableListTitle, expandableListDetail);

        expandableListView.setAdapter(expandableListAdapter);


        return viewGroup;


    }

}

 

 

실행을 해보면 순서가 FOOTBALL / CRICKET/ BASKETBALL

순으로 순서가 이상하게나옵니다..

유튜브보면서 천천히 검토 하면서 봤는데도 순서가 제대로 안되서 문의드립니다...

뽕짝 (390 포인트) 님이 2021년 9월 17일 질문
List<String> expandableListTitle를 생성하는 부분이 안보이네요. 아마도 expandableListDetail에서 데이터를 가져오면서 순서가 뒤바뀌는 것처럼 추측이 되는데요. 참고로 Map은 순서가 보장되지 않아요. 순서를 확실하게 처리하려면 SortedMap을 사용하셔야 해요. 그리고 님과 같은 경우는 parent-child구조이므로

public class SportGroup {
     private String name;
     private List<String> sports;
     // Constructor, getters & setters 생략
}

처럼, 가져가셔도 될 것 같은데요.

그리고 아이템을 가능하면 key값이 되는 필드를 하나 추가하세요. 그냥 텍스트는 키값으로 사용하기에 적합하지 않습니다. 언제 변경될지 모르니까요. 그리고 String 타입을 사용하기 보다는 클래스를 만드시는게 자연스러워 보입니다. String이 운동종목이나 운동종목그룹은 아니니까요.

public class SportGroup {
     private String id;
     private String name;
     private List<Sport> sports;
}

public class Sport {
    private String id;
    private String name;
    private String groupId;
}

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...