1

我从这个 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

日志“ouiouioui”显示:

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

日志“ouiouiouii”显示:

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

每次我在微调器中选择不同的设备时,日志都会显示最后一条消息“ouiouiouii”和“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;
}
}
4

2 回答 2

1

您需要通过 为项目设置标签setTag()。然后它会工作

于 2013-05-22T09:14:28.517 回答
0

您可以尝试将您的 onItemSelectedListener 更改为我已经实现的内容:

category_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {

                        String item_selected = arg0.getItemAtPosition(arg2)+"";//---------------This ***+""*** works as a toString() method
                    }

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

                    }
                });

另外,我不明白您为什么要设置选择,当您的操作肯定会触发此 onItemSelected 方法时。这就是您实施OnItemSelectedListener. 您不必在 onItemSelected 中设置选择。

public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)

在哪里,

parent = The AdapterView where the selection happened

view = The view within the AdapterView that was clicked

position = The position of the view in the adapter

id = The row id of the item that is selected

抱歉,我没有阅读您的评论部分。您希望从微调器中选择设备名称的用户名和密码,因此只需在 OnItemSelected() 中执行此操作,然后对选择时获得Devices tmp_device = devices.get(int position);的该对象执行任何您想做的事情。Devices

于 2013-05-22T09:39:32.017 回答