我使用listview
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("JSON RESULT VALUE");
adapter.add("JSON RESULT VALUE");
adapter.add("JSON RESULT VALUE");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
和JSON我的来源。
private TextView textView;
String strData = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONGet().execute();
}
private class JSONGet extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls);
HttpURLConnection urlConnection = null;
String result = "";
try {
URL url = new URL("my server url");
urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
if (code == 200) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
}
in.close();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray arr = new JSONArray(result);
for (int i= 0; i< arr.length(); i++) {
JSONObeject jo = arr.getJSONObject(i);
strData += jo.getString("name") + jo.getString("age");
}
} catch (JSONException e){
Log.d(TAG,"TTT");
}
textView = (TextView) findViewById(R.id.text);
textView.setText(strData);
但是这个setJSON显示的是textView。 我想要显示listView。
如何组合listview和JSON TEXT?
答案 0 :(得分:2)
您正在将异步任务的结果设置为onPostExecute方法内的文本视图,请将其替换为我的建议。您需要将这些结果添加到列表适配器并将该适配器设置为列表。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
try {
JSONArray arr = new JSONArray(result);
for (int i= 0; i< arr.length(); i++) {
JSONObeject jo = arr.getJSONObject(i);
String strData = jo.getString("name") + jo.getString("age");
adapter.add(strData);
}
} catch (JSONException e){
Log.d(TAG,"TTT");
}
// textView = (TextView) findViewById(R.id.text);
// textView.setText(strData);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
答案 1 :(得分:1)
您需要将数据添加到listview适配器,而不是将结果连接成单个字符串!
您可以修改onPostExcecute
,如下所示
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray arr = new JSONArray(result);
for (int i= 0; i< arr.length(); i++) {
JSONObeject jo = arr.getJSONObject(i);
adapter.add(jo.getString("name") + jo.getString("age"));
}
} catch (JSONException e){
Log.d(TAG,"TTT");
}
adapter.notifyDataSetChanged():
}