2

我正在开发一个应用程序,我必须在其中解析JSON数据并将它们放在 custom 中gridview。这是它应该是什么样子。

如何显示网格视图?

到目前为止,我已经在 asynctask 中解析了我的 JSON 数据并获取了这些值。这是我的代码:

private class getRedeemData extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdia = new ProgressDialog(AllPerksActivity.this);
        pdia.setMessage("Loading products. Please wait...");
        pdia.show();
    }

    @Override
    protected String doInBackground(String... args) {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
                HttpVersion.HTTP_1_1);
        HttpClient httpClient = new DefaultHttpClient(params);
        HttpPost post = new HttpPost(
                "MY API HERE..!!");

        post.setHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            post.setEntity(new StringEntity("client_id=" + client_id + "&"
                    + "client_secret=" + clientSecretKey, HTTP.UTF_8));

            HttpResponse response = httpClient.execute(post);
            int i = response.getStatusLine().getStatusCode();
            System.out.println("HTTP Post status AllPerk Redeemption API: "
                    + i);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            // SB to make a string out of the inputstream
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            // the json string is stored here
            String result = sb.toString();
            System.out.println("Result Body: " + result);
            return result;

        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        JSONObject jObject;
        try {
            jObject = new JSONObject(result);

            JSONArray jSearchData = jObject.getJSONArray("rewards");

            for (int i = 0; i < jSearchData.length(); i++) {

                JSONObject objJson = jSearchData.getJSONObject(i);

                String rewardID = objJson.getString("rewardID");
                String rewardType = objJson.getString("rewardType");
                String rewardTitle = objJson.getString("rewardTitle");

                System.out.println("Reward ID: " + rewardID);
                System.out.println("Reward Type: " + rewardType);
                System.out.println("Reward Tittle: " + rewardTitle);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        pdia.dismiss();
    }
}

这是我的 Gridview 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:verticalScrollbarPosition="right"
android:verticalSpacing="10dp" >
</GridView>

有人可以告诉我如何使用适配器来解决这个问题。

任何形式的帮助将不胜感激。

4

3 回答 3

2

您将 JSON 数据保存在某个数组中,然后

gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

请参阅此链接以获取带有自定义适配器的 Gridview

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

于 2013-03-26T11:10:29.983 回答
0

解析 JSON 数据后,将数据存储在一个集合中(ArrayList 即可),创建一个适配器并使用setAdapter网格视图的方法将其与您的 GridView 绑定

检查开发者文档

于 2013-03-26T11:09:38.497 回答
0

使用

import org.json.JSONArray;
import org.json.JSONObject;

可以轻松搞定。使用 json 数据检查参考Android gridview

于 2018-07-24T10:29:49.383 回答