ListView控件, 网络获取Json数据, 并将数据展示到listView中 的步骤:
- 先布局 (先对当前页面整体, 再为ListView中的每一个行进行单独的 行布局)
- 通过异步任务 从网络上 获取JSON串, 解析为对象,获得数据集合
- 编写ListView的适配器(借助适配器, 根据前面获得的数据集合来 为ListView框架 填充数据(text, image…))
1 布局无需多说
2.异步任务(AsyncTask类/相当于Thread类): 可以实现与UI(主线程)交互的线程框架。
为什么要使用异步任务 来执行网络获取操作?
Because:
使用:3个泛型,4个步骤 说明:
完整的 自定义异步任务:
class MyAsyncTask extends AsyncTask<String, Void, String>{
StringBuilder sb;
// "事前" 执行
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// "事中" 执行
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(1000 * 5);
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
byte[] bys = new byte[1024];
int len = 0;
sb = new StringBuilder();
while((len = is.read(bys)) != -1){
sb.append(new String(bys, 0, len));
}
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// "事后" 执行: 并接收 "事中"返回的 结果
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Food_Material food_material = gson.fromJson(s, Food_Material.class);
List<Food_Material.DishsBean> dishs = food_material.getDishs();
for (Food_Material.DishsBean dish:dishs) {
String img = dish.getImg(); //图片
// new MyImg().execute(img);
String name = dish.getName(); //1
String burdens = dish.getBurdens(); //2
String allClick = dish.getAllClick(); //4
String favorites = dish.getFavorites(); //5
Food_Material.DishsBean.CustomersBean customers = dish.getCustomers();
String nickName = customers.getNickName(); //3
beans.add(new Bean(img, name, burdens, nickName, allClick, favorites));
}
/**
* 适配器 填充数据
*/
MyBaseAdapter adapter = new MyBaseAdapter(beans, First_Activity.this);
Lv.setAdapter(adapter);
}
// 进度更新(不会自动调用, 需要手动调用) 一般与"事中"搭配使用: 用于UI界面的实时变化
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
3 适配器(Adapter):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(ct).inflate(R.layout.list_row, null);
ImageView img = convertView.findViewById(R.id.img);
TextView tv_01 = convertView.findViewById(R.id.name);
TextView tv_02 = convertView.findViewById(R.id.burdens);
TextView tv_03 = convertView.findViewById(R.id.nickName);
TextView tv_04 = convertView.findViewById(R.id.allClick);
TextView tv_05 = convertView.findViewById(R.id.favorites);
注意: // 该 语句 直接通过网址 下载图片, 然后 放到当前Position位置的ImageView/ImageButton 控件 !! Picasso.get().load(beans.get(position).getImg()).into(img);
tv_01.setText(beans.get(position).getName());
tv_02.setText(beans.get(position).getBurdens());
tv_03.setText(beans.get(position).getNickName());
tv_04.setText(beans.get(position).getAllClick()+"浏览");
tv_05.setText(beans.get(position).getFavorites()+"收藏");
}
return convertView;
}