如何从parseJson获取listview数据

时间:2015-10-04 12:17:30

标签: java android arrays json listview

嗨,请回答我的问题 我在Android开发的eclipse中有这个代码。 我正在使用mysql和php的数据库,并使用JSON获取数据。但我不知道如何在listview中使用JSONparse数据。请编辑我的代码。

public class ViewAllPersons extends Activity {

String url = "http://192.168.1.206/androhp/view_all_persons.php";
ArrayList<String> result;
ListView list;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_all_person);
    result = new ArrayList<String>();
    LoadAllPersons lap = new LoadAllPersons();
    lap.execute(url);
}

class LoadAllPersons extends AsyncTask<String, String, String> {

    protected String doInBackground(String... args) {
        InputStream jsonStream = getStreamFromURL(args[0], "GET");
        String jsonString = streamToString(jsonStream);
        parseJSON(jsonString);
        return null;
    }

    void parseJSON(String JSONString) {
        try {

            JSONObject jo = new JSONObject(JSONString);

            JSONArray allpersons = jo.getJSONArray("allpersons");
            for (int i = 0; i < allpersons.length(); i++) {
                JSONObject object = allpersons.getJSONObject(i);
                String objString = "";
                objString = object.getString("name") + " , "
                        + object.getString("name2") + " : "
                        + object.getInt("iconlink");
                result.add(objString);
            }

        } catch (JSONException e) {

        }
    }

    protected void onPostExecute(String file_url) { 

        list = (ListView) findViewById(R.id.list); 
           String[] web = {
                    "Google Plus",
                        "Twitter",
                        "Windows"
                } ;

                String[] imageUrl = {
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png"

                };
        CustomList adapter = new
               CustomList(ViewAllPersons.this, web, imageUrl);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}

如何使用parseJSON数据而不是web listview:

list = (ListView) findViewById(R.id.list); 
           String[] web = {
                    "Google Plus",
                        "Twitter",
                        "Windows"
                } ;

                String[] imageUrl = {
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
                        "http://www.varzesh3.com/football3_Images/varzesh3-logo.png"

                };

1 个答案:

答案 0 :(得分:1)

You have got both data as well as list, now you need to combine them.
first you have to convert your json result into list, where values are your json values.
    final ArrayList<String> listdata = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      listdata .add(values[i]);
    }

then you have to assign adapter to your list. You can use following code. 
  final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.yourlistlayout, listdata );
    list.setAdapter(adapter);

更多细节 http://www.vogella.com/tutorials/AndroidListView/article.html