What is a textViewResourceId?

独自空忆成欢 提交于 2019-12-22 05:10:40

问题


I've been trying to figure how the ArrayAdapter class can be used, and from the docs : http://developer.android.com/reference/android/widget/ArrayAdapter.html, I see the constructor expects an integer called textViewResourceId.

What is this exactly?

Edit: From a little more research and the answers here, it looks like it must be the id of a TextView that I defined in the xml file that contains the interface code. But I saw this example here:

ArrAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,myArrayList)

So how can android.R.layout.simple_list_item_1 be used here? What does this mean really? I am only familiar with using R.id.idOfMyViewHere


回答1:


it is the id of the textview in which the adapter will update the info you will provide. You can use textview provided by android:

android.R.id.text1

for instance. Or you can provide your own textView whit your custom id

Edit

change:

ArrayAdapter(this,android.R.layout.simple_list_item_1,myArrayList)

with

ArrayAdapter(this,android.R.id.text1,myArrayList)



回答2:


android.R.layout.simple_list_item_1 is default layout if you want to use your layout then you can also use it like

new ArrayAdapter<String>(context, layout_id, textview_id, items);




回答3:


textViewResourceId is an ID to a specific layout. To reference that id you make use of the format R.layout.* .

R.layout.* are the layouts that you have created found in your res/layout folder. So if you have a xml layout file in your res/layout/ folder named "my_list_item.xml" of whatever layout format you want, then you can use that as your textViewResourceId (not necessarily a TextView).

If you don't want to create your own layout, you can make use of the built in layouts found in your android-sdk directory, (in my case its C:\Android\android-sdk\platforms\android-8\data\res\layout). You reference those built-in layouts through android.R.layout.* (notice I started it with "android"). So if I want to use the built in simple_list_item_1.xml then I can reference that using android.R.layout.simple_list_item_1.

R.layout.* <---own layout
android.R.layout.* <--- built in Android layouts

Some popular layouts:
android.R.simple_list_item_1
simple_list_item_2.xml
etc.




回答4:


from the docs:

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.

simply, it default is bind to the textView, and the text view is

The id of the TextView within the layout resource to be populated



来源:https://stackoverflow.com/questions/10025773/what-is-a-textviewresourceid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!