简体   繁体   中英

How to combine two Adapter from call response in Android Java?

Is there a way to combine two adapters that are inside a Callback from Model? Like, I get data from two sources and I want to display both of them in the same recyclerView

I tried to load the method first then put both adapters on concatAdapter but I got a null from this concatAdapter and then my app crashed

here is my method in Java

private void fetchDataKabupaten(){
        progressBar.setVisibility(View.VISIBLE);
        token = getIntent().getStringExtra("token");
        Call<KabupatenModel> kabupatenModelCall = RetrofitClient.getLoginInterface().getKabupatenData("Bearer "+token);
        kabupatenModelCall.enqueue(new Callback<KabupatenModel>() {
            @Override
            public void onResponse(Call<KabupatenModel> call, Response<KabupatenModel> response) {
                kabupatenList = response.body().getKabupaten();
                adKabupaten = new KabupatenAdapter(MainActivity.this, kabupatenList);
                progressBar.setVisibility(View.GONE);
            }
}


private void fetchDataKecamatan(){
        progressBar.setVisibility(View.VISIBLE);
        token = getIntent().getStringExtra("token");
        Call<KecamatanModel> kecamatanModelCall = RetrofitClient.getLoginInterface().getKecamatanData("Bearer " + token);
        kecamatanModelCall.enqueue(new Callback<KecamatanModel>() {
            @Override
            public void onResponse(Call<KecamatanModel> call, Response<KecamatanModel> response) {
                kecamatanList = response.body().getKecamatan();
                adKecamatan = new KecamatanAdapter(MainActivity.this, kecamatanList);
                progressBar.setVisibility(View.GONE);
            }
}

I have tried to call both in MainActivity then setAdapter with concatAdapter like this:

fetchDataKabupaten();
fetchDataKecamatan();
concatAdapter = new ConcatAdapter(adKecamatan, adKabupaten);
rV.setAdapter(concatAdapter);

why is it null? but, when I only set one Adapter inside the response method it works.

you can only use one adapter in recyclerview, but adapter can have multiple view.

for example I have adapter for chat, with right and left view

Adapter Chat Detail

class AdapterChatDetail(val datas: ArrayList<ChatMessage>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var ctx: Context

companion object {
    const val LEFT = 0
    const val RIGHT = 1
}

inner class ViewHolderLeft(val binding: AdapterMyChatLeftBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(data: ChatMessage) {
        
    }
}

inner class ViewHolderRight(val binding: AdapterMyChatRightBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(data: ChatMessage) {
        
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    ctx = parent.context
    return when (viewType) {
        LEFT -> ViewHolderLeft(
            AdapterMyChatLeftBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
        RIGHT -> ViewHolderRight(
            AdapterMyChatRightBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
        else -> ViewHolderLeft(
            AdapterMyChatLeftBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    when (getItemViewType(position)) {
        LEFT -> (holder as ViewHolderLeft).bind(datas[position])
        RIGHT -> (holder as ViewHolderRight).bind(datas[position])
    }
}

override fun getItemCount() = datas.size

override fun getItemViewType(position: Int): Int {
    val data = datas[position]
    return if (data.position == "right") RIGHT else LEFT
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM