【问题标题】:how to get listview data from parseJson如何从 parseJson 获取列表视图数据
【发布时间】:2015-10-04 12:17:29
【问题描述】:

你好,请回答我的问题 我在 Eclipse 中有这个代码用于 Android 开发。 我正在使用 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"

                };

【问题讨论】:

  • 您创建的自定义适配器是否有任何错误?如果是,请添加那个 CustomList 代码?
  • 不,我在 custam 适配器中没有任何错误,只是我不知道如何使用 parseJSON 数据而不是 web listview。

标签: java android arrays json listview


【解决方案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

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多