0

光是写标题就难受了!我有一个微调器,它通过 JSON 从我的数据库中获取信息。它们有一个 name 列和一个 id 列。现在它只是在我的微调器上的一个列表中列出它们。我不需要显示 id,但是当他们选择一个项目时,它应该返回 id 供我使用。我敢肯定这已经被问过很多次了,但我找不到这种情况的任何例子。任何人都可以建议在我的代码中更改哪些内容吗?谢谢!

这是我当前的代码列表名称和下面的 ID。

List<String> items = new ArrayList<String>();

  @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

        int success;

        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));

            Log.d("request!", "starting");

            json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
            Log.d("Login attempt", json.toString());
            success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                mAlbums = json.getJSONArray(TAG_POSTS);

                // looping through all posts according to the json object
                // returned
                for (int i = 0; i < mAlbums.length(); i++) {
                    JSONObject c = mAlbums.getJSONObject(i);

                    // gets the content of each tag
                    String album_name = c.getString("album_name");
                    String album_code = c.getString("album_code");

                    items.add(album_name);
                    items.add(album_code);

                    Log.d("Login Successful!", json.toString());
                }

            } else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();

        ArrayAdapter<String> spinnerMenu = new ArrayAdapter<String>(
                MainOwnerScreen.this, android.R.layout.simple_list_item_1,
                items);
        mSpinner.setAdapter(spinnerMenu);

        if (file_url != null) {
            Toast.makeText(MainOwnerScreen.this, file_url,
                    Toast.LENGTH_LONG).show();
        }

    }

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    spinselected = (String) mSpinner.getItemAtPosition(position);
    Log.d("spin selected", spinselected);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
4

1 回答 1

0

Try to get the item with below code inside your onItemSelected

spinselected = items.get(position);
于 2013-09-26T19:42:03.553 回答