0

I am new to Kotlin and/or coding. The below mentioned are the code I made to populate a List View. But as I run the code, the activity_main.xml file is inflated but the text data are not attached.

Datasource.kt

class Datasource{

     companion object{
       val affirmationList: List<String> = listOf(
           "Affirmation_1", "Affirmation_2", "Affirmation_3", "Affirmation_4")} 
     

}

Here is the CustomAdapter.kt,

class CustomAdapter(val mContext:Context, val layoutId:Int, val textId: Int, val x: List<String>) :ArrayAdapter<Datasource>(mContext, layoutId, textId) {}

Here is MainActivity.kt

class MainActivity : AppCompatActivity()
 {
override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

// Item.View is the Id of ListView in activity_main.xml

    val itemListView= findViewById<ListView>(R.id.Item_View)

   // item_list is the Id of item_list.xml & Affirmation_Text is the Id of TextView in item_list.xml

    val customAdapterUse = CustomAdapter(this,R.layout.item_list, R.id.Affirmation_Text, Datasource.affirmationList)

    itemListView.adapter = customAdapterUse


}
}

2 Answers 2

0

You need to learn how the recycler view works. You haven't implemented any of the RecylerView adapter functions. I would recommend you watch some YouTube videos or tutorials on how to populate a list using RecyclerView. After you understand how the recycler view works, you can get some help from the documentation here.

Here is a code sample to give you an idea as to how your recycler view adapter should look like:


class CustomAdapter(private val dataSet: Array<String>) :
        RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView

        init {
            // Define click listener for the ViewHolder's View.
            textView = view.findViewById(R.id.textView)
        }
    }

    // Create new views (invoked by the layout manager)
    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        // Create a new view, which defines the UI of the list item
        val view = LayoutInflater.from(viewGroup.context)
                .inflate(R.layout.text_row_item, viewGroup, false)

        return ViewHolder(view)
    }

    // Replace the contents of a view (invoked by the layout manager)
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

        // Get element from your dataset at this position and replace the
        // contents of the view with that element
        viewHolder.textView.text = dataSet[position]
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = dataSet.size

}
Sign up to request clarification or add additional context in comments.

4 Comments

I am not asking about RecyclerView.Adapter, as I am not using that. I have only one TextView and trying to use simple ArrayAdapter to display a list of items. Would appreciate, if you or any of you can clarify why items are not getting attached to the list. I don't notice any error on the code. In Logcat, I see this, 'W/System: A resource failed to call close.'
Sorry, I will try to edit my code according to your needs. can you add your whole logcat error? the log that you provide does not provide any information.
@Jaguar Your adapter class is empty, you have to write these functions mentioned above. How should any viewholder know any information if you don' provide these infos.
as you would like to yous dynamic lists you can use also ListAdapter developer.android.com/guide/topics/ui/layout/recyclerview
0

Thanks for all of your feedback. I worked it out. Just changed CustomAdaptder class (added String type for ArrayAdapter and added data source variable x to the ArrayAdapter constructor. The final code is as below:

CustomAdapter(val mContext:Context, val layoutId:Int, val textId: Int, val x: List<String>) :ArrayAdapter<String>(mContext, layoutId, textId, x) {}

The code, which was not attaching data:

class CustomAdapter(val mContext:Context, val layoutId:Int, val textId: Int, val x: List<String>) :ArrayAdapter<Datasource>(mContext, layoutId, textId) {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.