我有一个ArrayAdapter
如下
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
这是我要用于我的 .我的文件Spinner
中的值集(字符串数组)的适配器。Spinner
strings.xml
如何务实地设置微调器<string-array>
?string.xml
请帮忙!
只需在创建时将其添加到您的 ArrayAdapter 中。
String[] yourArray = getResources().getStringArray(R.array.array_name);
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, yourArray);
你可以尝试这样的事情:
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);
希望这可以帮助!!!
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);
本教程: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 应该可以提供预期的结果。有关更多详细信息,请参阅教程。
如果您想从 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();
}
}
});
}
});
}
}
您可以在这篇文章中阅读完整信息:-将动态数据添加到微调器