使用Volley从ListView中的JSON获取图片的步骤如下:
dependencies {
implementation 'com.android.volley:volley:1.2.0'
}
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Item> itemList;
public CustomAdapter(Context context, List<Item> itemList) {
this.context = context;
this.itemList = itemList;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.image_view);
holder.textView = convertView.findViewById(R.id.text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Item item = itemList.get(position);
holder.textView.setText(item.getTitle());
// 使用Volley加载图片
ImageRequest imageRequest = new ImageRequest(item.getImageUrl(), new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
holder.imageView.setImageBitmap(response);
}
}, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
// 将ImageRequest加入到请求队列中
VolleySingleton.getInstance(context).addToRequestQueue(imageRequest);
return convertView;
}
private static class ViewHolder {
ImageView imageView;
TextView textView;
}
}
public class VolleySingleton {
private static VolleySingleton instance;
private RequestQueue requestQueue;
private static Context context;
private VolleySingleton(Context context) {
VolleySingleton.context = context;
requestQueue = getRequestQueue();
}
public static synchronized VolleySingleton getInstance(Context context) {
if (instance == null) {
instance = new VolleySingleton(context);
}
return instance;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request) {
getRequestQueue().add(request);
}
}
public class MainActivity extends AppCompatActivity {
private ListView listView;
private CustomAdapter adapter;
private List<Item> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
itemList = new ArrayList<>();
adapter = new CustomAdapter(this, itemList);
listView.setAdapter(adapter);
// 发起JSON请求,解析数据并添加到itemList中
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.getString("title");
String imageUrl = jsonObject.getString("image_url");
Item item = new Item(title, imageUrl);
itemList.add(item);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
// 将JsonObjectRequest加入到请求队列中
VolleySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
}
以上代码假设已经有一个Item类,用于存储每个ListView项的数据,包括标题和图片URL。在Activity中发起JSON请求,解析返回的数据,并将解析后的数据添加到itemList中。在自定义的Adapter中,使用Volley加载图片,并将图片设置到ImageView中。
注意:以上代码仅为示例,实际使用时需要根据具体情况进行适当的修改和优化。
领取专属 10元无门槛券
手把手带您无忧上云