Show image from remote database, with JSOn. Android

0

In a RecyclerView I show a list of data with image, this comes from a database that I have in 000webhost, where I store in a folder Images, images, PHP files and the business table with their respective name, category, description and route_image of business.

The following code is from the activity with the recycler:

 private void cargarwebservice() {
        progreso = new ProgressDialog(this);
        progreso.setMessage("Consultando...");
        progreso.show();
        String ip = getString(R.string.ip);
        String url = ip + "/DBRemota/wsJSONConsultarNegocio.php?categoria=" + categoria;
        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,this,this);
       // request.add(jsonObjectRequest);
        VolleySingleton.getIntanciaVolley(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
    }
    @Override
    public void onErrorResponse(VolleyError error) {
        progreso.hide();
        Toast.makeText(this,"No se pudo consultar....." + error.toString(), Toast.LENGTH_LONG).show();
        Log.i("ERROR" , error.toString());
        progreso.hide();
    }

    @Override
    public void onResponse(JSONObject response) {
        negocio negocio=null;

        JSONArray json=response.optJSONArray("negocio");

        try {

            for (int i=0; i<json.length(); i++){
                Toast.makeText(this,json.length() + "" ,Toast.LENGTH_SHORT).show();
                negocio=new negocio();
                JSONObject jsonObject=null;
                jsonObject=json.getJSONObject(i);

                negocio.setNombre(jsonObject.optString("nombre"));  //NOMBRE DEL ARCHIVO PHP
                negocio.setCategoria(jsonObject.optString("categoria"));
                negocio.setDescripcion(jsonObject.optString("descripcion"));
                negocio.setRutaimagen(jsonObject.optString("ruta_imagen"));
                negocio.setDireccion(jsonObject.optString("direccion"));
                negocio.setHorario(jsonObject.optString("horario"));

                listanegocio.add(negocio);

            }
            Toast.makeText(this,listanegocio.toString() ,Toast.LENGTH_SHORT).show();
            progreso.hide();
            listanegocioadapter adapter=new listanegocioadapter(listanegocio,getApplicationContext());   //CREAR RECYCLER
            adapter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {        //METODO ON CLICK DE LA LISTA MANDO lista PARA EL DETALLE
                    negocio Negocio = listanegocio.get(recyclernegocio.getChildAdapterPosition(v));
                    Intent intent = new Intent(Listanegocio.this, Listanegociodetalle.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("negocio",Negocio);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            recyclernegocio.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(this, "No se ha podido establecer conexión con el servidor" +
                    " "+response, Toast.LENGTH_LONG).show();
            progreso.hide();
        }

And the class listanegocioadapter:

public class listanegocioadapter extends RecyclerView.Adapter<listanegocioadapter.UsuariosHolder>
    implements View.OnClickListener {



        List<negocio> listaUsuarios;
       // RequestQueue request;
        Context context;
        private View.OnClickListener listener;


        public listanegocioadapter(List<negocio> listaUsuarios, Context context) {
            this.listaUsuarios = listaUsuarios;
            this.context = context;
            //request = Volley.newRequestQueue(context);   //HACER LA TRANSFORMACION A LA IMAGEN
        }

        @Override
        public UsuariosHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View vista= LayoutInflater.from(parent.getContext()).inflate(R.layout.negocio_list,parent,false);
            RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            vista.setLayoutParams(layoutParams);

            vista.setOnClickListener(this);

            return new UsuariosHolder(vista);
        }

        @Override
        public void onBindViewHolder(UsuariosHolder holder, int position) {
            holder.txtnombre.setText(listaUsuarios.get(position).getNombre());
            holder.txtcategoria.setText(listaUsuarios.get(position).getCategoria());
            holder.txtdescripcion.setText(listaUsuarios.get(position).getDescripcion());



            if (listaUsuarios.get(position).getRutaimagen()!=null){
                cargarImagenWebService(listaUsuarios.get(position).getRutaimagen(),holder);

            }else{
                holder.imagen.setImageResource(R.drawable.imagennodisponible);
            }



        }

    private void cargarImagenWebService(String rutaimagen , final UsuariosHolder holder) {
            String ip = context.getString(R.string.ip);
            String url = ip + "/DBRemota/"+ rutaimagen;
            url = url.replace(" " , "%20");

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                holder.imagen.setImageBitmap(response);
            }
        }, 0, 0, ImageView.ScaleType.CENTER, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,"Error al cargar la imagen..." , Toast.LENGTH_LONG);
            }
        });
       // request.add(imageRequest);
        VolleySingleton.getIntanciaVolley(context).addToRequestQueue(imageRequest);
    }

    @Override
        public int getItemCount() {
            return listaUsuarios.size();
        }



    public void setOnClickListener(View.OnClickListener listener){
            this.listener = listener;
    }
    public void onClick(View view) {
            if (listener!=null){
                listener.onClick(view);
            }

    }

    public class UsuariosHolder extends RecyclerView.ViewHolder{

            TextView txtnombre,txtcategoria,txtdescripcion;
            ImageView imagen;

            public UsuariosHolder(View itemView) {
                super(itemView);
                txtnombre= (TextView) itemView.findViewById(R.id.txtnombre);
                txtcategoria= (TextView) itemView.findViewById(R.id.txtcategoria);
                txtdescripcion= (TextView) itemView.findViewById(R.id.txtdescripcion);
                imagen = (ImageView) itemView.findViewById(R.id.imagen);

            }
        }
    }

The problem is that when I want to do the master pattern detail, I do not know how to pass the image

public class Listanegociodetalle extends AppCompatActivity {


    private TextView txnombre,txtdes,txthorario,txtdireccion;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listanegociodetalle);
        txnombre = (TextView) findViewById(R.id.nombre);
        txtdes = (TextView) findViewById(R.id.descripcion);
        imageView = (ImageView) findViewById(R.id.imagendetalle);
        txthorario = (TextView) findViewById(R.id.horario);
        txtdireccion = (TextView) findViewById(R.id.direccion);


        Bundle objeto = getIntent().getExtras();
        negocio Negocio = null;



        if (objeto!=null){
            Negocio = (negocio) objeto.getSerializable("negocio");
            txnombre.setText(Negocio.getNombre());
            txtdes.setText(Negocio.getDescripcion());
            txthorario.setText(Negocio.getHorario());
            txtdireccion.setText(Negocio.getHorario());
        }

    
asked by Ibarra Emiliano 31.12.2018 в 18:34
source

1 answer

0

To show the image you must pass it a int , but since it is in String , you convert it ("you wrap it") like this:

 imageView.setImageResource(Integer.parseInt(Negocio.getRutaimagen()));
    
answered by 02.01.2019 в 04:30