【问题标题】:Putting data from a jsonobject into a gridview将数据从 jsonobject 放入 gridview
【发布时间】:2016-05-20 00:31:57
【问题描述】:

我只是想知道如何将我从 json 文件中检索到的值放入 gridview,然后这个 gridview 在我的自定义适配器中。在任何时候,gridview 中只有 6 个 id 对应于图像和 6 个名称。

这是我的代码;

private TextView tvData;
private ImageView imgtest;
String ChampionName;
String ChampionNameInLowerCase;
String item2;
String item3;
private ListView Championinfo;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listview = (ListView) findViewById(R.id.listView);
    imgtest = (ImageView) findViewById(R.id.imageView);

    // http://api.champion.gg/champion/Ekko/
    new JSONTask().execute("http://api.champion.gg/champion/ekko/");

    Championinfo = (ListView)findViewById(R.id.listView);
}

public class JSONTask extends AsyncTask<String, String, List<Layoutmodel>> {
    @Override
    protected List<Layoutmodel> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;


        try {
            URL url = new URL(params[0]);

            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONArray jsonarray = new JSONArray(finalJson);
            List<Layoutmodel> LayoutModelList = new ArrayList<>();

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


                JSONObject finalObject = jsonarray.getJSONObject(i);

                Layoutmodel layoutmodel = new Layoutmodel();
                layoutmodel.setChampionName2(finalObject.getString("key"));
                layoutmodel.setRole(finalObject.getString("role"));

                ChampionName = finalObject.getString("key");
                String role = finalObject.getString("role");
                String items = finalObject.getString("items");

                JSONObject ItemArray = new JSONObject(items);
                item2 = ItemArray.getString("mostGames");
                JSONObject ItemArray2 = new JSONObject(item2);
                item3 = ItemArray2.getString("items");

                JSONArray jsonarray2 = new JSONArray(item3);
                StringBuffer TestBuffer = new StringBuffer();


                List<Layoutmodel.itemname> itemlist = new ArrayList<>();
                for (int j = 0; j < jsonarray2.length(); j++) {
                    JSONObject finalObject2 = jsonarray2.getJSONObject(j);

                    Integer ID = finalObject2.getInt("id");
                    String ItemName = finalObject2.getString("name");
                    TestBuffer.append(ID + "-" + ItemName + "\n");

                    Layoutmodel.itemname ItemNames = new Layoutmodel.itemname();
                    ItemNames.setName(ItemName);
                }
                layoutmodel.setItemnames(itemlist);
                layoutmodel.setRole(role);
                layoutmodel.setChampionName2(ChampionName);
                LayoutModelList.add(layoutmodel);

                return LayoutModelList;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    @Override
    protected void onPostExecute(List<Layoutmodel> result) {
        super.onPostExecute(result);
        LayoutAdapter adapter2 = new LayoutAdapter(getApplicationContext(), R.layout.rows, result);
        Championinfo.setAdapter(adapter2);




    }

}

公共类 LayoutAdapter 扩展 ArrayAdapter {

    private List<Layoutmodel> LayoutModelList;
    private int resource;
    private LayoutInflater inflater;

    public LayoutAdapter(Context context2, int resource, List<Layoutmodel> objects) {
        super(context2, resource, objects);
        LayoutModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position2, View convertView2, ViewGroup parent2) {
        if (convertView2 == null) {
            convertView2 = inflater.inflate(R.layout.rows, null);
        }
        ImageView imageofchamp;
        TextView nameofchamp;
        TextView position;
        GridView champitems;

        // imageofchamp = (ImageView) convertView2.findViewById(R.id.imageView);
        nameofchamp = (TextView) convertView2.findViewById(R.id.textView);
        position = (TextView) convertView2.findViewById(R.id.smalltxt);
       // champitems = (GridView) convertView2.findViewById(R.id.gridView);

        nameofchamp.setText(LayoutModelList.get(position2).getChampionName2());
        position.setText(LayoutModelList.get(position2).getRole());


        return convertView2;

    }
}

}

这是代码目前返回的内容; 这是上图的布局

这是我从 json 中实际获取项目 id 和名称的代码,

 for (int j = 0; j < jsonarray2.length(); j++) {
                JSONObject finalObject2 = jsonarray2.getJSONObject(j);

                Integer ID = finalObject2.getInt("id");
                String ItemName = finalObject2.getString("name");
                TestBuffer.append(ID + "-" + ItemName + "\n");

                Layoutmodel.itemname ItemNames = new Layoutmodel.itemname();
                ItemNames.setName(ItemName);
            }

【问题讨论】:

  • 你没有得到 gridview ?
  • 所以您想重新创建最后一张照片上的内容吗?
  • 是的,我会@DanielK
  • 您是否也从 JSON 中提取了 ChampName 和 position?
  • 是的,我做到了@DanielK

标签: java android json gridview


【解决方案1】:

要显示所有三个角色,请使用带有片段的选项卡式活动。 Here 是一个关于如何实现它的非常全面的最新教程。您已经知道如何从对象中提取 JSON 数据,因此您只需执行 3 次,每个角色一次。

【讨论】:

  • 我在看这个,我意识到我忘了提到一些非常关键的东西,我必须为 130 个英雄做这个,我必须把每个角色堆叠在一起跨度>
  • 这应该没什么区别。理想情况下,您回到之前的活动,该活动将包含冠军列表,然后用户将选择一个冠军,如回声,您的选项卡式活动将再次查询服务器并像我们讨论的那样处理数据。最多,您只需修改 ASyncTask 以获取用户在上一个活动中选择的英雄名称的参数。
猜你喜欢
  • 2014-05-28
  • 2013-05-04
  • 2015-06-29
  • 2012-09-22
  • 2020-01-23
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2020-02-23
相关资源
最近更新 更多