【发布时间】: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