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

スピナーで別のデバイスを選択するたびに、ログに最後のメッセージ「ouiouioui」と「ouiouiouii」が表示されるのはなぜですか? onItemSelected の各 JSONObject の値を取得するにはどうすればよいですか?

私のクラス Devices は次のようになります:

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

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

すみません、コメント欄を読んでいませんでした。スピナーから選択されたデバイス名のユーザー名とパスワードが必要なのでDevices tmp_device = devices.get(int position);、OnItemSelected() でこれを行いDevices、選択したこのオブジェクトで必要なことを行います。

于 2013-05-22T09:39:32.017 に答える