how to pass a lot of data to SimpleAdapter (Android | Kotlin)

97 Views Asked by At
  1. How to pass all elements from "data" to "simpleData" for SimpleAdapter? For instance we have 100+ elements, it is not resonable to write elements by one. (should i use a loop? but how?)
  2. TITLE doesn't show properly when simpleAdapter has used:[https://i.stack.imgur.com/W1kgJ.png]

hope you can help me, thanks in advance <3

    val data = listOf<Book>(
            Book("Memory", R.drawable.cover_1),
            Book("The king of drugs", R.drawable.cover_2),
            Book("SIN", R.drawable.cover_4),
            Book("The martian", R.drawable.cover_5),
            Book("No place like here", R.drawable.cover_6),
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater).also { setContentView(it.root) }
        super.onCreate(savedInstanceState)
        setupMyBookAdapter()
        setupMySimpleAdapter()
    }

    private fun setupMySimpleAdapter() {

        val simpleData = mutableListOf(

                    mapOf(TITLE to data[0].title, PHOTO to data[0].thumbnail),
                    mapOf(TITLE to data[1].title, PHOTO to data[1].thumbnail),
                    ...
                    ...
                    mapOf(TITLE to data[n].title, PHOTO to data[n].thumbnail)

        )

        val adapter2 = SimpleAdapter(this, simpleData, R.layout.cardview_item, arrayOf(TITLE, PHOTO), intArrayOf(R.id.book_title,R.id.book_cover))
        binding.mylistview2.adapter = adapter2
    }


 
1

There are 1 best solutions below

0
Style-7 On

Set title and cover of books as equal resources, for example R.string.title_0, R.drawable.cover_0. Next use loop:

int i=0;
while( true ){
    int id = getResources().getIdentifier( "title_"+i, "string", getPackageName() );
    if( id > 0){
        String title = getString(id);
        // add to List here                
        i++;
    }else{
        break;
    }
}