Android Error

Android RecyclerView-selection tracker java.lang.IllegalArgumentException Error

 

0. Summary

0-1. dependency

androidx.recyclerview:recyclerview-selection:1.1.0-beta01

0-2. Logic

val tracker = SelectionTracker.Builder<Long>(
                "ChoiceTimeTableTracker",
                rv_bottom_share_content,
                RecyclerViewIdKeyProvider(rv_bottom_share_content),
                ChoiceTimetableLookUp(rv_bottom_share_content),
                StorageStrategy.createLongStorage()
 ).withSelectionPredicate(SelectionPredicates.createSelectSingleAnything()).build()

val bottomChoiceTimetableAdapter = BottomChoiceTimetableAdapter()
rv_bottom_share_content.adapter = bottomChoiceTimetableAdapter
        
bottomChoiceTimetableAdapter.setSelectionTracker(tracker)
bottomChoiceTimetableAdapter.submitList(arrayListOf("0", "1"))
rv_bottom_share_content.layoutManager = LinearLayoutManager(context)        

1. Error Message

java.lang.IllegalArgumentException
        at androidx.core.util.Preconditions.checkArgument(Preconditions.java:36)
        at androidx.recyclerview.selection.SelectionTracker$Builder.<init>(SelectionTracker.java:508)

위와 같은 에러를 발견했습니다.

SelectionTracker.Builder를 잘못 만들어서 발생한 에러인 줄 알았습니다. 하지만, 아니었습니다.

해당 오류는 StackOverFlow에서 힌트를 얻을 수 있었습니다. 해당 오류가 발생한 이유는, SelectionTracker를 사용할 때,아직 RecyclerView에 Adapter가 연결되지 않았기 때문입니다.

 

2. Solved

val bottomChoiceTimetableAdapter = BottomChoiceTimetableAdapter()
rv_bottom_share_content.adapter = bottomChoiceTimetableAdapter

val tracker = SelectionTracker.Builder<Long>(
	"ChoiceTimeTableTracker",
	rv_bottom_share_content,
	RecyclerViewIdKeyProvider(rv_bottom_share_content),
	ChoiceTimetableLookUp(rv_bottom_share_content),
	StorageStrategy.createLongStorage()
    ).withSelectionPredicate(SelectionPredicates.createSelectSingleAnything()).build()
bottomChoiceTimetableAdapter.setSelectionTracker(tracker)
bottomChoiceTimetableAdapter.submitList(arrayListOf("0", "1"))
rv_bottom_share_content.layoutManager = LinearLayoutManager(context)

SelectionTracker.Builder에서 사용되어지는 RecyclerView에는 Adapter가 연결되어 있어야 한다.

그래서, SelectionTracker를 선언하기 전에, RecyclerView에 Adapter를 먼저 연결하면 된다.