我是新编程,所以我请求任何熟悉 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 从这个活动传递到列表视图。