Jiaen Ong
Jiaen Ong

Reputation: 41

How to display image in listview from database using volley

I want to display a list of image using listview, the image will be get from the server which show at the below picture, i know the way is using volley but i just want to show image only without other attribute, is it possible to do that? i will be appreciated if anyone can help it out, below is the sample code i write.enter image description here

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);
    }

Upvotes: 0

Views: 669

Answers (2)

Omkar
Omkar

Reputation: 3100

try below code

 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;
}

Upvotes: 0

ibhavikmakwana
ibhavikmakwana

Reputation: 10121

Use Glide or Picasso library to do so.

For Picasso:

Picasso.with(context).load("your image url").into(imageView);

and For Glide:

GlideApp.with(this).load("your image url").into(imageView);

You have to write this code to your adapter.

Upvotes: 1

Related Questions