Issue
I am having a massively hard time getting to grips with Kotlin. The learning curve for me seems to be especially hard.
I am trying to write a simple app for my son to track his homework. I have a recycler view that displays the 25 homework tasks he has. He's allowed to complete them in any order so I thought a recycler was perfect for this. After much pulling out of hair, I managed to get the recycler working. I don't claim to fully understand what is happening, but it is working and it's returning the list nicely. Screenshot
Now that I have this, with the headline information from the tasks, I want my son to be able to click on the task (maybe long press) and have the full details of the task popup.
I understand that by using popupWindow I can use, pretty much, any layout. My layout is going to have TextViews and maybe ImageViews and buttons of Select (i.e. select this task) and Complete (to mark off that he's done it).
I read this question and it seems to be very close to what I want to do. Popup inside a recycler View. I was able to make the changes in my original Adapter
class AllTasksAdapter(
private val chooserAdapterCallback: ChooserAdapterCallback,
private val mList:List<ItemsViewModel>)
:RecyclerView.Adapter<AllTasksAdapter.ViewHolder>() {
class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
val subjectImage: ImageView = itemView.findViewById(R.id.subjectImage)
val txtTaskName: TextView = itemView.findViewById(R.id.txtTaskName)
val txtTaskRequirements: TextView = itemView.findViewById(R.id.txtTaskRequirements)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// inflates the card_view_design view that is used to hold list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.card_view_design, parent, false)
return ViewHolder(view)
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val t = mList[position]
// sets the views from our itemHolder class
if(t.subjectID == 1) //English/Topic
{ holder.subjectImage.setImageResource(R.drawable.icons8_knowledge_sharing_80) }
else if(t.subjectID == 2) //Maths
{ holder.subjectImage.setImageResource(R.drawable.icons8_maths_64) }
else if(t.subjectID == 3) //Science
{ holder.subjectImage.setImageResource(R.drawable.icons8_test_tube_80) }
else if(t.subjectID == 4) //Reading
{ holder.subjectImage.setImageResource(R.drawable.icons8_reading_80) }
else if(t.subjectID == 5) //Spelling
{ holder.subjectImage.setImageResource(R.drawable.icons8_broken_pencil_80) }
holder.txtTaskName.text = t.name
holder.txtTaskRequirements.text = t.requirements
holder.subjectImage.tooltipText = t.subject
holder.itemView.setOnClickListener {
chooserAdapterCallback.onChooseAdapterClick(t)
}
}
//return the number of the items in the list
override fun getItemCount(): Int {
return mList.size
}
interface ChooserAdapterCallback{
fun onChooseAdapterClick(t: ItemsViewModel)
}
}
This seemed to go OK, but I'm stuck now.
The original question says "...modify your Adapter initialization and pass this there so roughly as ChooserAdapter(this, chooseList)". My call, inside the onCreate of MainActivity, to populate the adapter (before I added the second parameter) was:
val adapter = AllTasksAdapter(data)
allTasksRecycler.adapter = adapter
I have data defined further up as:
val data = ArrayList<ItemsViewModel>()
So the two things I'm struggling with are: (1) What I provide for the first parameter (2) How I link the recycler with the call to open the popup
The original question talks about the ChooserActivity and the MusclePopup in the context of the original post, but I can't work out what the equivalents in my code are.
The recycler in my code is AllTasksRecycler. This is inflating a layout called card_view_design.xml and the layout I want to use for displaying the details is called single_task_details.xml
I think I'm close, but I just need a little help getting over the line with this part. Could someone give me a shove in the right direction please?
Solution
In the example you are following, the adapter + ChooserAdapterCallback are being implemented by the ChooserActivity class. Passing this as constructor parameter won't work.
interface ChooserAdapterCallback{
fun onChooseAdapterClick(t: ItemsViewModel)
}
If you want to kind of reuse your approach, pass the context to your adapter, like this, see below. Otherwise take exactly the same approach as in the other question you've provided.
class AllTasksAdapter(
private val context: Activity,
private val mList:List<ItemsViewModel>)
:RecyclerView.Adapter<AllTasksAdapter.ViewHolder>() {
Now, instantiate your adapter in the desired activity, where "this" refers to the activity/context.
val adapter = AllTasksAdapter(this, data)
In the onBindViewHolder method, use, where CustomDialog is just the Dialog class:
holder.itemView.setOnClickListener {
CustomDialog(this, character).show()
}
For example,
class CustomDialog(
private val adapter: AllTasksAdapter,
private val taskItem: Task // or however your model is called
): Dialog(adapter.context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.fragment_pop_up)
setupComponents()
}
private fun setupComponents() {
findViewById<TextView>(R.id.input_chooser).text = taskItem.name // add here the attributes you want to show.
}
}
Answered By - timacosta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.