我想使用listview显示图像列表,图像将从服务器获取,如下图所示,我知道使用凌空的方式,但我只是想显示图像而没有其他属性,是吗有可能吗?如果有人能帮忙,我将不胜感激,下面是我写的示例代码。
public class ImageAdapter extends ArrayAdapter<subjectFile> {
private final List<subjectFile> list;
Activity context;
public ImageAdapter(Activity context, int resource, List<subjectFile> list)
{
super(context, resource, list);
this.list = list;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image, parent, false);
//ImageLoader imageLoader;
ImageView imageViewInitial = (NetworkImageView) view.findViewById(R.id.imageViewPresent);
//imageLoader = Controller.getPermission().getImageLoader();
subjectFile subjectfile = getItem(position);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageBytes = baos.toByteArray();
imageBytes = Base64.decode(subjectfile.getImage(), Base64.DEFAULT);
Bitmap decodeImage = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
imageViewInitial.setImageBitmap(decodeImage);
return view;
}
}
private void fetchSubject(Context context, String url) {
RequestQueue queue = Volley.newRequestQueue(context);
swipeRefreshLayout.setRefreshing(true);
final JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
list.clear();
for (int i = 0; i < response.length(); i++) {
JSONObject imageResponse = (JSONObject) response.get(i);
String id = imageResponse.getString("subjectID");
String name = imageResponse.getString("name");
String image = imageResponse.getString("image");
String type = imageResponse.getString("type");
String description1 = imageResponse.getString("Description1");
String description2 = imageResponse.getString("Description2");
String version = imageResponse.getString("version");
subjectFile subjectfile = new subjectFile(id, name, image, type, description1, description2, version);
list.add(subjectfile);
}
loadImageFiles();
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), "Error:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
swipeRefreshLayout.setRefreshing(false);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity().getApplicationContext(), "Error" + volleyError.getMessage(), Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity().getApplicationContext(), "Empty", Toast.LENGTH_LONG).show();
volleyError.printStackTrace();
swipeRefreshLayout.setRefreshing(false);
}
});
queue.add(jsonObjectRequest);
}
答案 0 :(得分:1)
毕加索:
Picasso.with(context).load("your image url").into(imageView);
和Glide:
GlideApp.with(this).load("your image url").into(imageView);
您必须将此代码写入适配器。
答案 1 :(得分:0)
尝试以下代码
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image, parent, false);
ImageView imageViewInitial = (ImageView) view.findViewById(R.id.imageViewPresent);
String imgString = list.get(position).getImage();
byte[] decodeString = Base64.decode(imgString , Base64.DEFAULT);
Bitmap decoded = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
imageViewInitial.setImageBitmap(decoded);
return view;
}