如何让我的JSON进入android listview?(how do i get my JSON into android listview? [duplicate])

编程入门 行业动态 更新时间:2024-10-28 22:30:45
如何让我的JSON进入android listview?(how do i get my JSON into android listview? [duplicate])

这个问题在这里已有答案:

JSON Android和Listview 3答案

我是一个新的程序员,我正在制作一个应用程序,可以从MYSQL获取数据到PHP,然后在Android上显示。 我一直试图找到一个解决方案,但到目前为止我看到的教程似乎都没有为我工作,我只是设法从json获得一个对象到一个textview。 但我真正需要的是将数据显示在listview上的各行上。

这是我的JSON输出,

[{"id":"1","name":"darrel","password":"pass1234"},{"id":"2","name":"garrett","password":"important"},{"id":"3","name":"neoys","password":"yseniopass"},{"id":"4","name":"john","password":"mikel123"},{"id":"5","name":"owen","password":"mike4l"}]

和我的java代码,只有一个用户显示在textview上。

package com.darre.jsonreader; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.ListActivity; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @TargetApi(Build.VERSION_CODES.GINGERBREAD) public class Users extends ListActivity { /** Called when the activity is first created. */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //listView.setOnItemClickListener(new OnItemClickListener() { // public void onItemClick(AdapterView<?> parent, View view, // int position, long id) { // When clicked, show a toast with the TextView text // Toast.makeText(getApplicationContext(), // ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://172.30.54.153/databases/"); TextView textView = (TextView)findViewById(R.id.textView1); ListView listview = (ListView)findViewById(R.id.listView1); try { HttpResponse response = httpclient.execute(httppost); String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); JSONArray mArray = new JSONArray(jsonResult); for (int i = 0; i < mArray.length(); i++) { JSONObject object = mArray.getJSONObject(i); String name = object.getString("name"); String password = object.getString("password"); textView.setText(name + " - " + password); } } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

提前致谢!!!

This question already has an answer here:

JSON Android And Listview 3 answers

I'm a new programmer and I'm making an app which can get data from MYSQL to php and then display on android. I've been trying to find a solution but none of the tutorials I've seen so far seems to work for me, I've only managed to get one object from json into a single textview. But what I really need is to get data to be displayed on individual rows on listview.

here's my JSON output,

[{"id":"1","name":"darrel","password":"pass1234"},{"id":"2","name":"garrett","password":"important"},{"id":"3","name":"neoys","password":"yseniopass"},{"id":"4","name":"john","password":"mikel123"},{"id":"5","name":"owen","password":"mike4l"}]

and my java code which gets only one of the users displayed onto a textview.

package com.darre.jsonreader; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.ListActivity; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @TargetApi(Build.VERSION_CODES.GINGERBREAD) public class Users extends ListActivity { /** Called when the activity is first created. */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //listView.setOnItemClickListener(new OnItemClickListener() { // public void onItemClick(AdapterView<?> parent, View view, // int position, long id) { // When clicked, show a toast with the TextView text // Toast.makeText(getApplicationContext(), // ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://172.30.54.153/databases/"); TextView textView = (TextView)findViewById(R.id.textView1); ListView listview = (ListView)findViewById(R.id.listView1); try { HttpResponse response = httpclient.execute(httppost); String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); JSONArray mArray = new JSONArray(jsonResult); for (int i = 0; i < mArray.length(); i++) { JSONObject object = mArray.getJSONObject(i); String name = object.getString("name"); String password = object.getString("password"); textView.setText(name + " - " + password); } } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

Thanks in advance!!!

最满意答案

您可以阅读本教程,它解释了实现它的方法,第一个是“直接”List适配器,第二个是自定义List的方法。

http://www.mkyong.com/android/android-listview-example/

此外,您不应该使用JSON数据,首先,您必须为每个Item创建一个Object,然后使用某种List(例如ArrayList)对其进行分组。

You can read this tutorial, it explains to ways of implement it, the first, a "direct" List adapter, the second, the way to customize your List.

http://www.mkyong.com/android/android-listview-example/

Also, you shouldn't work with JSON data, first, you have to create an Object for each Item, and then group it with some kind of List (ArrayList, for example).

更多推荐

本文发布于:2023-08-05 05:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:android   JSON   duplicate   listview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!