Android显示从json到spinner和textview的数据

时间:2014-01-25 07:51:48

标签: android json

大家好我想从我的服务器显示数据并在我的微调器和文本视图上显示主题。到目前为止,我尝试过的是在我的微调器上显示它们。但我不知道如何在textview上显示它们。

这是我到目前为止所尝试的:

private ArrayList<Verse> versesList;
versesList = new ArrayList<Verse>();

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(ALARM.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Downloading Verses");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
        // Create the array 
        // Retrieve JSON Objects from the given website URL in JSONfunctions.class

        ServiceHandler jsonParser = new ServiceHandler();
        String result;

        lang = Locale.getDefault().getLanguage();

        if(lang.equals("en")){
            result = jsonParser.makeServiceCall(URLENGLISH, ServiceHandler.GET);
        } else  {
            result = jsonParser.makeServiceCall(URLTAGALOG, ServiceHandler.GET);
        }


        try {
            JSONArray jr = new JSONArray(result);

            Log.d("Response: ", "> " + result);

            for (int i = 0; i < jr.length(); i++) {
                JSONObject catObj = jr.getJSONObject(i); //(JSONObject) jr.get(i);
                Verse cat = new Verse(catObj.getInt("verse_id"), 
                        catObj.getString("verse_title"));

                    versesList.add(cat);

            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
} 

@Override
protected void onPostExecute(Void args) {
        // Close the progressdialog
        if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();
        populateSpinner();
    }
}

/**
 * Adding spinner data
 * */
private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < versesList.size(); i++) {
        lables.add(versesList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(spinnerAdapter);
}

有人知道如何实现这个目标吗?对你的帮助表示感谢。感谢。

1 个答案:

答案 0 :(得分:1)

So far what I've tried is displaying them on my spinner. But I don't have a clue how to display them on textview.

要在textview中显示所选项目,您需要获取所选项目的位置

 int position = spinner.getSelectedItemPosition();

然后

String content = versesList.get(position).getContent().toString();

然后

textView.setText(content);