How to fill a ListView with a string-array?

前端 未结 2 1594
你的背包
你的背包 2021-01-04 17:27

I want to show these itens at my ListView


    Google
    Bing         


        
相关标签:
2条回答
  • 2021-01-04 17:46

    You can do this with an ArrayAdapter and have it use either an Array or a List to give it data

    lv.setAdatper(new ArrayAdapter<String>(context, R.layout.some_layout_to_use, R.id.some_textview_in_layout, listData);
    
    0 讨论(0)
  • 2021-01-04 18:03

    You can create your adapter with a one liner, check out the static method ArrayAdapter createFromResource(Context context, int textArrayResId, int textViewResId)

    The first argument will probably be your Activity, the second is R.array.bookmark_titles, and the third is a layout to use.

    To clarify based on the comments, the method accepts int, which is exactly what the constants in your generated R class are stored as.

    Here's a complete example, assuming this is being called from an Activity:

    ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this, R.array.bookmark_titles, android.R.layout.simple_list_item_1);
    myListView.setAdapter(aa);
    

    In this case, android.R.layout.simple_list_item_1 refers to an XML layout that is provided by the Android SDK. You can change this if needed.

    0 讨论(0)
提交回复
热议问题