android - Spinner Android onItemSelected 只需选择列表的最后一项

标签 android spinner

我从这个 JSON 填充一个微调器:

[
    {
        "vhc_name": "",
        "vhc_login": "",
        "vhc_password": ""
    },
    {
        "vhc_name": "Tél de Pan-K",
        "vhc_login": "178143p",
        "vhc_password": "kaspersky"
    },
    {
        "vhc_name": "toto",
        "vhc_login": "215058k",
        "vhc_password": "azertyu"
    },
    {
        "vhc_name": "azertyuiop",
        "vhc_login": "221589a",
        "vhc_password": "azertyu"
    }
]

它工作正常,但是当我在微调器上使用 setOnItemSelectedListener 时,它只检索最后一个 JSONObject 的数据:

{
    "vhc_name": "azertyuiop",
    "vhc_login": "221589a",
    "vhc_password": "azertyu"
}

知道它是从哪里来的吗?

这是我的代码:

private static final String TAG_LOGIN = "vhc_login";
private static final String TAG_PWD = "vhc_password";

         String readFeed = readFeed();
// Display the list of the user's devices
ArrayList<Devices> devices = new ArrayList<Devices>();
// use this array to populate myDevicesSpinner
ArrayList<String> devicesNames = new ArrayList<String>();

try {

  JSONArray jsonArray = new JSONArray(readFeed); // Method which parse the JSON Data


  for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    Devices device = new Devices();
    device.setName(jsonObject.optString(TAG_NAME));
    devices.add(device);
    devicesNames.add(jsonObject.optString(TAG_NAME));

   }
} catch (Exception e) {
}
mySpinner = (Spinner)findViewById(R.id.myDevicesSpinner);
mySpinner.setAdapter(new ArrayAdapter<String>(this,     android.R.layout.simple_spinner_dropdown_item, devicesNames));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
Devices tmp_device = devices.get(i);
        pwd = tmp_device.getPassword();
        Log.d("testoui",pwd);
}
public void onNothingSelected(AdapterView<?> adapterView) {

} 
}); 

日志“ouioui”显示:

11:41:08.539    9081    com.JeLocalisetrackerApp.view   DEBUG   ouioui  kaspersky
11:41:08.539    9081    com.JeLocalisetrackerApp.view   DEBUG   ouioui  azertyu
11:41:08.539    9081    com.JeLocalisetrackerApp.view   DEBUG   ouioui  azertyu

日志“ouiouii”显示:

11:41:08.539    9081    com.JeLocalisetrackerApp.view   VERBOSE ouiouii 178143p
11:41:08.539    9081    com.JeLocalisetrackerApp.view   VERBOSE ouiouii 215058k
11:41:08.539    9081    com.JeLocalisetrackerApp.view   VERBOSE ouiouii 221589a

日志“ouiouiioui”显示:

11:41:08.585        9081    com.JeLocalisetrackerApp.view   VERBOSE ouiouioui   azertyu

日志“ouiouiouii”显示:

11:41:08.585        9081    com.JeLocalisetrackerApp.view   VERBOSE ouiouiouii  221589a

每次我在微调器中选择不同的设备时,日志都会显示最后一条消息“ouiouiioui”和“ouiouiouii” 为什么?如何检索 onItemSelected 中每个 JSONObject 的值?

我的类设备是这样的:

public class Devices {

 String name;
 String logindevice;
 String passworddevice;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getLogin() {
    return logindevice;
}
public void setLogin(String logindevice) {
    this.logindevice = logindevice;
}    

public String getPassword() {
    return passworddevice;
}
public void setPassword(String passworddevice) {
    this.passworddevice = passworddevice;
}
}

最佳答案

您需要通过setTag() 为项目设置标签。 然后就可以了

关于android - Spinner Android onItemSelected 只需选择列表的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16687699/

相关文章:

android - 在运行时获取 Android 屏幕尺寸类型(drawable-small、drawable-normal 等)

javascript - Rails 5,等待/加载屏幕,直到模型准备好

vba - 修改用户表单以在多个工作表上工作

android - 如何管理android微调器项目的高度?

Android 布局编辑器在预览自定义 View 时抛出异常

android - Sencha Android 原生应用

android - 适用于 Android 的双微调器

java - Android:Spinner onItemSelect 膨胀另一个 Spinner

android - 光标位置来到编辑文本的开始

android - Flutter 中任务 ':app:stripDebugDebugSymbols' 执行失败