java - 将数据从 jsonobject 放入 gridview

标签 java android json gridview

我只是想知道如何将从 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);




    }

}

公共(public)类 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;

    }
}

}

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

这是我实际从 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);
            }

最佳答案

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

关于java - 将数据从 jsonobject 放入 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336160/

相关文章:

java - 将倒数计时器值传递给下一个 Activity

MediaPlayer.isplaying() 方法中的 java.lang.IllegalStateException

android - 方向更改时出现多个 Smart Lock 对话框

java - Java 如何处理 alpha channel ?

java - 如何在android中对JSONArray进行排序

android - 收到通知后重新启动应用程序

php - json_encode 不会转义换行符

json - 在 Swift 中解析 JSON 不起作用

python - 无返回时处理 JSON 解码错误

java - ARCore中,如何让节点/ anchor 粘在相机上?