使用JSON填充Spinner

时间:2015-01-12 20:27:29

标签: android json spinner

我已经为Android找到了这个精彩的JSON解析tutorial,因为我对这个领域还不熟悉,我想问一下如何用给定数据填充一个微调器。当用户点击名称时,其余部分将加载,然后当用户选择一个名称时,它必须保持活动状态。任何帮助都会很有帮助!

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // we will using AsyncTask during parsing
        new AsyncTaskParseJson().execute();
    }

    // you can make this class as another java file so it will be separated from your main activity.
    public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

        final String TAG = "AsyncTaskParseJson.java";

        // set your json string url here
        String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";

        // contacts JSONArray
        JSONArray dataJsonArr = null;

        @Override
        protected void onPreExecute() {}

        @Override
        protected String doInBackground(String... arg0) {

            try {

                // instantiate our json parser
                JsonParser jParser = new JsonParser();

                // get json string from url
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                // get the array of users
                dataJsonArr = json.getJSONArray("Users");

                // loop through all users
                for (int i = 0; i < dataJsonArr.length(); i++) {

                    JSONObject c = dataJsonArr.getJSONObject(i);

                    // Storing each json item in variable
                    String firstname = c.getString("firstname");
                    String lastname = c.getString("lastname");
                    String username = c.getString("username");

                    // show the values in our logcat
                    Log.e(TAG, "firstname: " + firstname
                            + ", lastname: " + lastname
                            + ", username: " + username);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String strFromDoInBg) {}
    }
}

3 个答案:

答案 0 :(得分:1)

感谢大家的帮助,但真正的解决方案是需要在OnPostExecute()中添加以下代码:

Spinner yourSpinner= (Spinner) findViewById(R.id.yourSpinner);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(<YourActivityNAme>.this, android.R.layout.simple_spinner_item, namesList);
yourSpinner.setAdapter(adapter);

答案 1 :(得分:0)

从json创建列表,这样

List<String> list = new ArrayList<String>();
list.add(json element);
list.add(json element);
list.add(json element);


ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
spinner.setAdapter(dataAdapter);

答案 2 :(得分:0)

Spinner使用列表对象来提供数据。所以你应该有一个Spinner对象的支持列表。

在for循环之前,请执行以下操作:

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

在for循环中,将名称添加到此列表中:

//I'm assuming you are going to display only the first name
namesList.add(firstName);

然后创建一个ArrayAdapter对象,将列表绑定到Spinner。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, namesList);
yourSpinner.setAdapter(adapter);