How to fix this ArrayAdapter requires the resource ID to be a TextView

喜夏-厌秋 提交于 2019-12-02 14:12:13

Since you are using the default ListView and ArrayAdapter, and you have only one data list, ie SubCatgyData, you should be using android.R.layout.simple_list_item_1 instead of android.R.layout.simple_list_item_2.

The difference between the two is android.R.layout.simple_list_item_1 has only one TextView inside it, while android.R.layout.simple_list_item_2 has two TextView.

ArrayAdapter adapter = new ArrayAdapter(Sub_Categories.this,android.R.layout.simple_list_item_1,SubCatgyData);

You need to override [ArrayAdapter.getView(int, View, ViewGroup)](http://developer.android.com/reference/android/widget/ArrayAdapter.html#getView(int, android.view.View, android.view.ViewGroup)). See http://developer.android.com/reference/android/widget/ArrayAdapter.html

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

Create your custom layout for adapter. You are using one provided by android. See the android prefix you are using for your layout.

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