【问题标题】:How to map json array elements to option selected in Spinner?如何将 json 数组元素映射到 Spinner 中选择的选项?
【发布时间】:2017-04-19 15:51:04
【问题描述】:

我有一个像下面这样的 json 数组,并且想在从 spinner 中选择一个选项时选择相应的 id,该选项也是动态的,即也是一个显示在 Spinner 中的 json 数组

{
   "DoctorName": ["0001 DR. Sameer", "0001 DR.Krishna murti", "110 Mr. Ram", "4 Mr. Yash Pathak.", "99 Dr. Varma"],
    "DoctorId": [3,2,110,4,99]
};

而且我必须在 Android 中这样做。任何帮助将不胜感激。

【问题讨论】:

  • 你的json无效..请改正

标签: android arrays json spinner


【解决方案1】:

您必须使用ArrayAdapter 将 json 数组值显示到微调器中

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_values); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

//Set on item select Listener
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

             // here you can get your selected id

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

了解更多check here.

【讨论】:

  • 你能指定什么是list_values吗?名称数组还是 id 数组?
  • list_values 只不过是您必须在此处传递的 json 数组值
  • 实际上我有 2 个 json 数组,即 json 数组应该传递的“DoctorName”和“DoctorId”
  • 查看我更新的答案,首先你必须改变你的 json 格式它是无效的。把它变成一个数组,就像一个 id 一个名字,然后试试上面的代码。
  • 是的,两个数组的记录数相同。如何获取 Id 你能以编程方式提供帮助吗? @普拉蒂克达萨
【解决方案2】:
  1. 创建两个数组,即DoctorName和DoctorId
  2. 使用上述数组创建动态HashMap,使用for循环将所有值以key-value形式。但为此,上述两个数组的长度应该相同。

    HashMap<String, String> hash;
    for(int i = 0; i < DoctorName.size() ; i++) {
    
        hash = new HashMap<String, String>();
        hash.put(DoctorId.get(i), DoctorName.get(i));
    }
    
  3. 对于微调器,仅从地图(哈希)发送医生姓名列表,微调器的 onclick 获取其 Id,即医生 Id。 在 Spinner onclick 中编写以下代码

    String name = spinner.getSelectedItem().toString();
    String id = hash.get(name);
    

    在id中你会得到所选名字对应的id。

希望对你有帮助:)

【讨论】:

  • 在“hash.put(DoctorId.get(i), DoctorName.get(i))”上得到错误
【解决方案3】:

1.首先创建一个类

 public class DoctorName
  {

public String id = "";
public String name = "";

public void setId(String id)
{
    this.id = id;
}

public void setName(String name)
{
    this.name = name;
}


public String getName()
{
    return name;
}

public String getId()
{
    return id;
}

// A simple constructor for populating our member variables for this tutorial.
public DoctorName( String _id, String _name)
{
    id = _id;
    name = _name;

}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name );
}

}

2.创建另一个类 MainClass.java

ArrayList<DoctorName> doctList = new ArrayList<DoctorName>() ;

    for(int i=0;i<arr_name.length;i++)
    {
        doctList.add(new DoctorName(arr_id[i],arr_name[i]));
    }

    //fill data in spinner
    //ArrayAdapter<DoctorName> adapter = new ArrayAdapter<DoctorName>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, answers);
    ArrayAdapter <DoctorName>adapter= new ArrayAdapter<DoctorName>
            (getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,doctList );

    Doctor_selection.setAdapter(adapter);

    Doctor_selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {

            DoctorName doctorName = (DoctorName) parent.getSelectedItem();
            Log.i("SliderDemo", "getSelectedItemId" +doctorName.getId());

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
        }
    });

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多