-1

I have recyclerview with checkbox and I want to checklist all the data using button. I have trying this tutorial, but when i click the button, the log is call the isSelectedAll function but can't make the checkbox checked. what wrong with my code?

this is my adapter code

var isSelectedAll = false

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListApproveDeatilViewHolder {
    val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.activity_list_approve_row, parent, false)

    return ListApproveDeatilViewHolder(itemView)
}

private lateinit var mSelectedItemsIds: SparseBooleanArray

fun selectAll() {
    Log.e("onClickSelectAll", "yes")
    isSelectedAll = true
    notifyDataSetChanged()
}

override fun onBindViewHolder(holder: ListApproveDeatilViewHolder, position: Int) {
    val approve = dataSet!![position]

    holder.soal.text = approve.title
    holder.kategori.text = approve.kategori

    if (!isSelectedAll){
        holder.checkBox.setChecked(false)
    } else {
        holder.checkBox.setChecked(true)
    }
}

and this is my activity code

override fun onCreate(savedInstanceState: Bundle?) {
     private var adapter: ListApproveDetailAdapter? = null

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_list_approve)
    ButterKnife.bind(this)

    getData()

    // this is my button onclick code

    select.setOnClickListener(){
        if (select.getText().toString().equals("Select all")){
            Toast.makeText(this, "" + select.getText().toString(), Toast.LENGTH_SHORT).show()
            adapter?.selectAll()
            select.setText("Deselect all")

        } else {
            Toast.makeText(this, "" + select.getText().toString(), Toast.LENGTH_SHORT).show()
            select.setText("Select all")

        }
    }

}
//this is for get my data for the recyclerview
fun getData() {
    val created_by = intent.getStringExtra(ID_SA)
    val tgl_supervisi = intent.getStringExtra(TGL_SURVEY)
    val no_dlr = intent.getStringExtra(NO_DLR)

    API.getListApproveDetail(created_by, tgl_supervisi, no_dlr).enqueue(object : Callback<ArrayList<ListApprove>> {
        override fun onResponse(call: Call<ArrayList<ListApprove>>, response: Response<ArrayList<ListApprove>>) {
            if (response.code() == 200) {
                tempDatas = response.body()
                Log.i("Data Index History", "" + tempDatas)
                recyclerviewApprove?.setHasFixedSize(true)
                recyclerviewApprove?.layoutManager = LinearLayoutManager(this@ListApproveActivity)
                recyclerviewApprove?.adapter = ListApproveDetailAdapter(tempDatas)
                adapter?.notifyDataSetChanged()
            } else {
                Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_LONG).show()
            }

            swipeRefreshLayout.isRefreshing = false

        }

        override fun onFailure(call: Call<ArrayList<ListApprove>>, t: Throwable) {
            Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_SHORT).show()

            swipeRefreshLayout.isRefreshing = false

        }
    })

}

this is the log when i click the button

thankyou for any help :)

Athira
  • 1,177
  • 3
  • 13
  • 35
Ratri
  • 337
  • 1
  • 7
  • 21
  • I think the problem is you have messes up `adapter` instance . You already created `adapter` inside `onCreate` and you are setting a new adapter inside `getData()`. This is why `notifyDataSetChanged` not worked . – ADM Nov 26 '18 at 08:19
  • Replace `recyclerviewApprove?.adapter = ListApproveDetailAdapter(tempDatas)` with `recyclerviewApprove?.adapter = adapter` – ADM Nov 26 '18 at 08:21
  • add boolean field to your model class and set its value to true or false and based on that set your checkbox – karan Nov 26 '18 at 08:21
  • @ADM i have replaced `recyclerviewApprove?.adapter = ListApproveDetailAdapter(tempDatas)` with `recyclerviewApprove?.adapter = adapter` but i get null data for recyclerview – Ratri Nov 26 '18 at 08:27
  • @KaranMer sorry sir, how i set my checkbox? i don't understand – Ratri Nov 26 '18 at 08:30
  • you set your checkbox as you are now, but add one boolean to `ListApprove` class. change its value true/false as per your condition. – karan Nov 26 '18 at 08:33
  • @KaranMer may i have your email? i want to ask the detail because i'm confused – Ratri Nov 26 '18 at 08:51
  • you can continue here. – karan Nov 26 '18 at 08:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184256/discussion-between-ratri-and-karan-mer). – Ratri Nov 26 '18 at 08:54
  • Why are you not passing _selectAll_ flag to adapter constructor and check by it? – Piyush Nov 26 '18 at 10:10

3 Answers3

0

As i already mentioned in comments you are using two different adapter instance . Now i see you have declared adapter globally . Just modify your code as follows and make sure response.body() have data int it :

 if (response.code() == 200) {
            tempDatas = response.body()
            Log.i("Data Index History", "" + tempDatas)
            recyclerviewApprove?.setHasFixedSize(true)
            recyclerviewApprove?.layoutManager = LinearLayoutManager(this@ListApproveActivity)
            adapter = ListApproveDetailAdapter(tempDatas)
            recyclerviewApprove?.adapter=adapter
        } else {
            Toast.makeText(this@ListApproveActivity, "Error", Toast.LENGTH_LONG).show()
        }
ADM
  • 20,406
  • 11
  • 52
  • 83
  • do I have to delete my private var adapter: ListApproveDetailAdapter? = null declaration? – Ratri Nov 26 '18 at 09:38
  • NO!! Keep it . Do not use `ListApproveDetailAdapter(tempDatas)` multiple times . – ADM Nov 26 '18 at 09:51
  • yes i have changed the code and i can get the data but still can checked when i click the button sir – Ratri Nov 26 '18 at 09:54
0

Add one variable in model class.

like var isSelect : Boolean

In your selectAll() method update adpter list and notify adapter.

Edit: in the adapter class.

if (approve.isSelect){
        holder.checkBox.setChecked(true)
    } else {
        holder.checkBox.setChecked(false)
    }

Hope this may help you.

OR

If you are using AndroidX then use should use one recyclerview features.

androidx.recyclerview.selection

A RecyclerView addon library providing support for item selection. The library provides support for both touch and mouse driven selection. Developers retain control over the visual representation, and the policies controlling selection behavior (like which items are eligible for selection, and how many items can be selected.)

Reference from here

Mitesh Vanaliya
  • 2,491
  • 24
  • 39
0

I am posting the answer with implementation of demo project. I haven't modified your code but as per your requirement i have done this.

MainActivity class:

class MainActivity : AppCompatActivity() {

var selectAll: Boolean = false;

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) as RecyclerView

    val btnSelectAll = findViewById<Button>(R.id.btnSelectAll) as Button


    //adding a layoutmanager
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)


    //crating an arraylist to store users using the data class user
    val users = ArrayList<User>()

    //adding some dummy data to the list
    users.add(User("Piyush", "Ranchi"))
    users.add(User("Mehul", "Chennai"))
    users.add(User("Karan", "TamilNadu"))
    users.add(User("Bela", "Kolkata"))

    //creating our adapter
    val adapter = CustomAdapter(users, selectAll)

    //now adding the adapter to recyclerview
    recyclerView.adapter = adapter

    btnSelectAll.setOnClickListener {

        if (!selectAll) {
            selectAll = true
        } else {
            selectAll = false
        }
        adapter?.selectAllCheckBoxes(selectAll)
    }
    }
    }

User class:

data class User(val name: String, val address: String)

Adapter class:

class CustomAdapter(val userList: ArrayList<User>, val selectAll: Boolean) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

var selectAllA = selectAll;


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
    return ViewHolder(v)
}


override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.textViewName.text = userList[position].name;

    if (!selectAllA){
        holder.checkBox.setChecked(false)
    } else {
        holder.checkBox.setChecked(true)
    }
}

//this method is giving the size of the list
override fun getItemCount(): Int {
    return userList.size

}

//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val textViewName = itemView.findViewById(R.id.textViewUsername) as TextView
    val checkBox = itemView.findViewById(R.id.checkbox) as CheckBox
}

 fun selectAllCheckBoxes(selectAll: Boolean) {
     selectAllA = selectAll
     notifyDataSetChanged()
}

}
Piyush
  • 18,895
  • 5
  • 32
  • 63