0

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

 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 从这个活动传递到列表视图。

4

2 回答 2

0

您可能需要一个 ArrayAdapter 或从 BaseAdapter 扩展的类,然后通过 adpater 的构造函数传递您的数据(如 JSONObject 或其他东西)。所以你可以将它绑定到你的列表视图。

于 2013-07-20T03:23:28.427 回答
0

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

于 2013-11-21T03:42:05.690 回答