我有ArrayAdapter
如下
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
这是我将用于Spinner
的适配器。我的Spinner
文件中有strings.xml
的值集(字符串数组)。
如何实际将<string-array>
string.xml
设置为微调器?请帮忙!
答案 0 :(得分:4)
创建时只需将其添加到ArrayAdapter即可。
String[] yourArray = getResources().getStringArray(R.array.array_name);
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, yourArray);
答案 1 :(得分:4)
您可以尝试这样的事情:
String[] mTestArray;
ArrayAdapter<String>dataAdapter;
mTestArray = getResources().getStringArray(R.array.testArray);
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,mTestArray);
然后将适配器连接到Spinner。
spinner.setAdapter(dataAdapter);
希望这有帮助!!!
答案 2 :(得分:2)
本教程:Android Spinners指出您将使用以下代码根据资源设置Spinner数组:
ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(this,
R.array.string-array-id, android.R.layout.simple_spinner_item);
dataAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
使用createFromResource()
方法而不是new
并提供数组的资源ID应提供预期的结果。有关更多详细信息,请参阅教程。
答案 3 :(得分:2)
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
String[] itemNames = getResources().getStringArray(R.array.stringArrayName);
for (int i = 0; i < 16; i++) // Maximum size of i upto --> Your Array Size
{
dataAdapter.add(itemNames[i]);
}
spinner2.setAdapter(dataAdapter);
答案 4 :(得分:1)
如果要从Web服务加载数据并将其设置在微调器中,则可以使用以下代码:-
public class MainActivity extends AppCompatActivity {
Spinner spinner;
List<String> usersList;
ArrayAdapter<String> catAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
usersList = new ArrayList<String>();
catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usersList);
spinner.setAdapter(catAdapter);
getHttpResponse();
}
public void getHttpResponse() {
String url = "https://cakeapi.trinitytuts.com/api/listuser";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String mMessage = e.getMessage().toString();
Log.w("failure Response", mMessage);
//call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String mMessage = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("Response", mMessage);
try {
JSONObject users = new JSONObject(mMessage);
JSONArray usersArr = users.getJSONArray("users");
for(int i =0; i < usersArr.length(); i++){
JSONObject user = usersArr.getJSONObject(i);
Log.e("user", user.getString("username"));
usersList.add(user.getString("username"));
}
catAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
}
您可以阅读这篇文章的完整信息:-Add dynamic data to spinner