[英]How do i change my spinner ArrayAdapter<String> into a hashmap?
How do i change my spinner from string into a hashmap? 如何将微调器从字符串更改为哈希图? I am getting my data in spinner from my database using json.
我正在使用json从我的数据库中的Spinner中获取数据。 I want to pass by id everytime a select a spinner data.
我想每次选择微调器数据时都通过ID。 The problem is i dont know how to change my spinner into a hashmap.
问题是我不知道如何将微调器更改为哈希图。 Help me pls.
请帮助我。 Thank you!
谢谢!
Here is my spinner 这是我的微调器
final List<String> list1 = new ArrayList<String>();
spinner1 = (Spinner) findViewById(R.id.sp1);
ArrayAdapter<String> spinner = new ArrayAdapter<String>(Games.this, layout.simple_spinner_dropdown_item, list1);
spinner1.setAdapter(spinner);
spinner1.setSelection(0);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch (position) {
case 1:
intent = new Intent(getApplication(), Basketball.class);
startActivity(intent);
break;
Here is how i add data to my spinner 这是我如何向微调器添加数据
try
{
URL url = new URL (Config.URL_SPIN);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
is = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
while((line = bufferedReader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
JSONArray JA = new JSONArray(result);
JSONObject json;
s_name = new String[JA.length()];
s_gender = new String[JA.length()];
for(int i = 0; i<JA.length(); i++)
{
json = JA.getJSONObject(i);
s_gender[i] = json.getString("s_gender");
s_name[i] = json.getString("s_name");
}
list1.add("All");
for(int i = 0; i<s_name.length; i++)
{
list1.add(s_name[i] + " "+s_gender[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
spinner_fn();
}
To use hashmap with arrayadapter you need list with hashmap type: 要将hashmap与arrayadapter一起使用,您需要使用hashmap类型的列表:
List<Object> list = new ArrayList<>();
And pass this list to your adapter: 并将此列表传递给您的适配器:
ArrayAdapter<Object> spinner = new ArrayAdapter<>(Games.this, layout.simple_spinner_dropdown_item, list);
hope this may helps you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.