Thursday, 16 May 2019

RecyclerView with two queries

I am making a RecyclerView with two TextViews inside each row the the first TextView is the name the second is how many items this user has of a certain item.

I am using parse-platform db and my schema is like the following:

Users table:
objectId (String)
name  (String)
....
Items table:
objectId  (String)
item_name  (String)
count  (Number)
.....
User_item table:
objectId (String)
user_pointer  (Pointer)
item_pointer  (Pointer)
item_status  (String)
item_notes  (String)
......

The way I am trying to get how many item each user has is by using the countInBackground method and save the number to an arraylist but to do that I have to get the IDs of each users first by a query then get the number of items by another query.

When I try that using the following code the app crash giving java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Can some point me what I need to do to solve this?


method code in kotlin:

private fun getData(itemID: String?) {

        personsQuery.findInBackground { result, e ->
            if (e == null) {
                for (i in result.indices) {
                    val id = result[i].objectId //get the objectID
                    personIDDataList.add(id)    //add the objectID to the array list

                    val elementUsers = result[i].getString("name")
                    if (elementUsers != null) {
                        personDataList.add(elementUsers)
                    } else {
                        personDataList.add("")
                    }

                    val itemCountQuery = ParseQuery<ParseObject>("User_item")

                    val itemPointer = ParseObject.createWithoutData("Items", itemID)
                    itemCountQuery.whereEqualTo("item_pointer", itemPointer)

                    for (i2 in personIDDataList.indices) {
                        val userPointer = ParseObject.createWithoutData("Users", personIDDataList[i2])
                        itemCountQuery.whereEqualTo("user_pointer", userPointer)

                        itemCountQuery.countInBackground { count, error ->
                            if (error == null) {
                                countDataList.add(count)
                            } else {
                                Toast.makeText(context, resources.getString(R.string.something_went_wrong), Toast.LENGTH_LONG)
                                    .show()
                                Log.e("error", error.toString())
                            }
                        }
                    }
                }
            } else {
                Toast.makeText(context, resources.getString(R.string.something_went_wrong), Toast.LENGTH_LONG).show()
            }

            view?.recyclerView?.adapter = RVAdapter(personDataList, countDataList, this)
        }

    }

Adapter code in kotlin :

class PartsUsersRVAdapter(
    internal var part: ArrayList<String>,
    internal var count: ArrayList<Number>,
    private val mOnClickListener: ListItemClickListener
) : RecyclerView.Adapter<PartsUsersRVAdapter.ViewHolder>() {

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(position)
    }

    override fun getItemCount(): Int {
        return part.size
    }

    interface ListItemClickListener {
        fun onListItemClick(clickedItemIndex: Int)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        var txtPersonName: TextView = itemView.part_user_list_name_text
        var txtCount: TextView = itemView.part_user_list_count_text

        init {
            itemView.setOnClickListener(this)
        }

        fun bind(listIndex: Int) {
            txtPersonName.text = part[listIndex]
            txtCount.text = count[listIndex].toString()
        }

        override fun onClick(v: View) {
            val clickedPosition = adapterPosition
            mOnClickListener.onListItemClick(clickedPosition)
        }
    }
}

Update:

I was able to bypass the crash by adding countDataList.add(0) right after personDataList.add(elementUsers) but this is not good for code at all.



from RecyclerView with two queries

No comments:

Post a Comment