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

리스트뷰에 버튼하나만 추가하고 싶습니다.

0 추천

현재 textView로만 구성된 리스트뷰를 구성하였는데요. 리스트뷰 맨 마지막에 버튼을 하나 추가하고 싶습니다.

 

버튼 객체를 코드로 생성 후 adapter.add를 할려고 했는데 이게 아닌 것 같습니다.

 

public class Chest extends ListFragment {

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

        ArrayList<String> values = new ArrayList<String>(Arrays.asList(
                "벤치프레스", "인클라인 벤치프레스", "디클라인 벤치프레스"
                ,"덤벨 프레스", "인클라인 덤벨 프레스", "디클라인 덤벨 프레스",
                "딥스", "케이블 크로스오버", "플라이 펙 덱", "풀 오버"));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

        
        return v;
    }

 

매력적인수박 (670 포인트) 님이 2021년 7월 12일 질문

1개의 답변

0 추천
 
채택된 답변

개발자 문서에 ArrayAdapter의 아이템을 세팅할 수 있는 메소들이 나와 있습니다.

https://developer.android.com/reference/android/widget/ArrayAdapter

void	add(T object)
Adds the specified object at the end of the array.

void	addAll(T... items)
Adds the specified items at the end of the array.

void	addAll(Collection<? extends T> collection)
Adds the specified Collection at the end of the array.

void	clear()
Remove all elements from the list.

님의 경우는 어댑터에 String타입을 받고 있기 있기 때문에 Button을 추가할 수는 없습니다.

ArrayAdapter Item에 공통적으로 사용할 수 있는 클래스를 하나 만드세요.

public enum AdapterItemType {
    WORKOUT,
    BUTTON
}

public class AdapterItem {
    private final String text;
    private final AdapterItemType itemType; 
    //or
    @LayoutRes private final int layoutId;
    
    public AdapterItem(String text, AdapterItemType itemType) {
        this.text = text;
        this.itemType = itemType;
    }

    public String getText() {
        return text;
    }

    public AdapterItemType getItemType() {
        return itemType;
    }
}


ArrayList<AdapterItem> values = new ArrayList<>(Arrays.asList(
    new AdapterItem("벤치프레스", AdapterItemType.WORKOUT),
    new AdapterItem("인클라인 벤치프레스", AdapterItemType.WORKOUT),
    ...
));
 
ArrayAdapter<AdapterItem> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);

adapter.add(new AdapterItem("Click Me", AdapterItemType.BUTTON))

ArrayAdapter 안에서 AdapteritemType에 따라 아래 메소드들을 오버라이드 하셔서 뷰를 그려주는 방법을 달리하시면 됩니다.

AdapterItem에 layout resource id 를  추가하셔서 Layout 을  inflate 하셔도 좋을 것 같네요.

getDropDownView(int position, View convertView, ViewGroup parent)

getView(int position, View convertView, ViewGroup parent)

spark (224,800 포인트) 님이 2021년 7월 12일 답변
매력적인수박님이 2021년 7월 13일 채택됨
chest.class가 ListFragment를 상속하고 있어서 ArrayAdapter를 상속할 수 없는데 이럴 때는 어떻게 해야 하나요...?
ListFragment는 ListAdapter를 리턴하고 ArrayAdapter는 ListAdapter를 구현했기 때문에  크게 문제 될게 없어 보이는데요. ListFragment의 getListAdapter() 메소드를 오버라이드해서 ArrayAdapter를 리턴해주면 될 것 같은데요.

추가로 왜 ListFragment랑 ArrayAdapter를 쓰고 계신지 처음부터 좀 궁금했었는데요.

ListFragment
This package is part of the Android support library which is no longer maintained. The support library has been superseded by AndroidX which is part of Jetpack. We recommend using the AndroidX libraries in all new projects. You should also consider migrating existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library class mappings.

구글 문서에 ListFragment는 Support라이브러에 존재하는 클래스라서 Jetpack으로 옮겨가면서 더이상 유지보수를 안한다고 되어있어요. 리스크가 큰 클래스를 굳이 사용할 필요가 있을까 싶네요.

그냥 Fragment에 RecyclerView를 사용하시는게 보통 사용하는 방법입니다.  Adapter는 요구사항에 따라ListAdapter, ContactAdapter 정도를 사용하시면 되구요.
감사합니다. 제가 코린이라 잘 몰랐습니다. 공식 문서를 보는게 익숙하지가 않네요. 덕분에 많이 배웠습니다. recyclebview로 구현해보겠습니다. 감사합니다.
...