ArrayAdapter.createFromResource issue

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I am trying to make "Select one" on Spinner. I saw all answer regarding this subject but I am still having some issues. Usual way of making custom spinner is:

ArrayAdapter<CharSequence> dataAdapter1 =  ArrayAdapter.createFromResource(this, R.array.entries,                                         android.R.layout.simple_spinner_item);         dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);         spinner1.setAdapter(dataAdapter1);          spinner1.setAdapter(                   new NothingSelectedSpinnerAdapter(                         dataAdapter1,                         R.layout.contact_spinner_row_nothing_selected,                                                     this)); 

In this code I have to define R.array.entries in Strings.xml, but my app is populating spinner from MySQL and I am having a list grad[i]=json.getString("Grad");. How can I create this ArrayAdapter.createFromResource with that list instead of Entries that are defined in Strings.xml? Tnx

回答1:

Query the data, put it in a List or Array and use this constructor of Array Adapter

ArrayAdapter<CharSequence> dataAdapter1 =  new ArrayAdapter(this,                                      android.R.layout.simple_spinner_item, yourArrayOrList); 

More here: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, java.util.List)



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