android将json对象传递给listview

时间:2013-07-20 03:08:55

标签: android json listview

我是新编程所以我要求任何熟悉JSONArray和ArrayAdapters以及listviews的人提供一些帮助。我只需要有人给出一个在我的情况下将JSONObject从活动传递到listview的示例。

 public class WhatsYourQuestion extends Activity implements OnClickListener {


    EditText et; 

    TextView answer; 





    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(InstanceState);

        setContentView(R.layout.main);



        et = (EditText) findViewById(R.id.editText1);

        Button myButton = (Button) findViewById(R.id.button1);

        myButton.setOnClickListener(new OnClickListener()  {


            @Override

            public void onClick(View v) {
            new asynctask().execute();
            }
     }

       public class asynctask extends AsyncTask<String, String, String>{

           protected void onPreExcute(String q) {
             q = et.getText();
             }

       @Override
       protected String doInBackground(String... params) {
              static InputStream is = null;
              static JSONObject jObj = null;
              static String json = "";
              try{
                 HttpClient client = new DefaultHttpClient();
                 URI website = new URI("http://example" + q.setText +"json");
                 HttpGet request = new HttpGet();
                 request.setURI(website);
                 HttpResponse response = client.execute(request);
                 HttpEntity httpEntity = response.getEntity();
                 is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

               }

        }


        JSONParser jParser = new JSONParser();


        JSONObject json = jParser.getJSONFromURI(uwebsite);

        try[
           ResultsSet = json.getJSONArray(TAG_RESULTSSET);

           for(int i = 0; i < ResultsSet.length(); i++){
           JSONObject r = ResultsSet.getJSONObject(i);


           String Subject = r.getString(TAG_SUBJECT);
           String NumAnswers = r.getString(TAG_NUMANSWERS);


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


        protected void OnPostExecute(String results){
        ??????

现在我需要有人给我一个如何将我的JSONObject从此活动传递到列表视图的示例。

2 个答案:

答案 0 :(得分:0)

您可能需要一个ArrayAdapter或类来自BaseAdapter,然后通过adpater的构造函数传递您的数据(如JSONObject或其他内容)。所以你可以把它绑定到你的列表视图。

答案 1 :(得分:0)

本教程对您的问题非常有用。但可能会在一些代码中进行编辑。 androidhive.info/2012/05/how-to-connect-android-with-php-mysql /