Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更改微调器阵列自适应<;字符串>;进入哈希映射?_Android_Spinner - Fatal编程技术网

Android 如何更改微调器阵列自适应<;字符串>;进入哈希映射?

Android 如何更改微调器阵列自适应<;字符串>;进入哈希映射?,android,spinner,Android,Spinner,如何将微调器从string更改为hashmap?我使用json从数据库中获取spinner中的数据。每次选择微调器数据时,我都要传递id。问题是我不知道如何将微调器更改为hashmap。请帮帮我。谢谢大家! 这是我的纺纱机 final List<String> list1 = new ArrayList<String>(); spinner1 = (Spinner) findViewById(R.id.sp1); ArrayAdapter<String&g

如何将微调器从string更改为hashmap?我使用json从数据库中获取spinner中的数据。每次选择微调器数据时,我都要传递id。问题是我不知道如何将微调器更改为hashmap。请帮帮我。谢谢大家!

这是我的纺纱机

 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;
final List list1=new ArrayList();
喷丝头1=(喷丝头)findViewById(R.id.sp1);
ArrayAdapter微调器=新的ArrayAdapter(Games.this,layout.simple\u微调器\u下拉菜单\u项,列表1);
喷丝头1.固定接头(喷丝头);
喷丝头1.1(0);
spinner1.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener()){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
意向=无效;
开关(位置){
案例1:
intent=newintent(getApplication(),Basketball.class);
星触觉(意向);
打破
下面是如何将数据添加到微调器

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();


    }
试试看
{
URL=新URL(Config.URL\u SPIN);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
is=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”);
StringBuilder sb=新的StringBuilder();
而((line=bufferedReader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
尝试
{
JSONArray JA=新JSONArray(结果);
JSONObject json;
s_name=新字符串[JA.length()];
s_gender=新字符串[JA.length()];

对于(int i=0;i要将hashmap与arrayadapter一起使用,您需要具有hashmap类型的列表:

List<Object> list = new ArrayList<>();
List List=new ArrayList();
并将此列表传递给您的适配器:

ArrayAdapter<Object> spinner = new ArrayAdapter<>(Games.this, layout.simple_spinner_dropdown_item, list);
ArrayAdapter微调器=新的ArrayAdapter(Games.this,layout.simple\u微调器\u下拉菜单\u项,列表);

希望这对您有所帮助。

创建您自己的自定义适配器类。另一个类?我会将所有内容放在那里,先生,检索数据?不,您不需要将所有内容放在那里。您想要的是ArrayAdapter而不是ArrayAdapter?是的,先生,当我在数据库中获取数据时,这可能吗?我在这里遇到问题,先生列表1。添加(“全部”);for(int i=0;isays列表不能应用于(java.lang.string)切换到列表,否则您将很难处理常规装箱和取消装箱规则。切换到列表将允许您添加字符串和HashMap。Piyus给出的答案与您想要的非常接近。您必须添加HashMap而不是字符串。只需一个简单的问题,为什么您需要特别使用HashMap?to将数据库中的id传递到下一个活动,先生,我有一个关于如何通过hashmap传递的代码。我只是不知道如何将字符串转换为hasmap