我只是想知道如何将我从json文件中检索的值放入gridview,然后这个gridview在我的自定义适配器中。在任何时候,只有6个id对应于图像,并且在gridview中对应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);
}
答案 0 :(得分:1)
要显示所有三个角色,请使用带有片段的选项卡式活动。 Here是一个非常全面,最新的如何实现它的教程。您已经知道如何从对象中提取JSON
数据,因此您只需要执行3次,每个角色一次。