How to show an image when we move from activity in a ListView

0

I'm having problems showing an image when I click on a list and activity step. The problem I have, I think, in the MainActivity.java line intent.getParcelableExtra("foto", datos.getFoto()); I think I'm having a hard time the parameters to upload the photo. Then he sent everything to the second activity SecondActivity.java where I try to load the photo to show, but it does not load at any time.

I do not know what I can be doing wrong to not show the image.

File - MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         ListView lista = (ListView)findViewById(R.id.listView_lista);
        final ArrayList<Datos> arraydatos = new ArrayList<>();
        Datos datos;

        //Introduzco los datos
        datos = new Datos(getResources().getDrawable(R.drawable.foto1), "Foto numero 1", "Imagen de tipo png", 1);
        arraydatos.add(datos);
        datos = new Datos(getResources().getDrawable(R.drawable.foto2), "Foto numero 2", "Imagen de tipo png", 2);
        arraydatos.add(datos);


        //Creo el adapter personalizado
         AdapterDatos adapter = new AdapterDatos(this, arraydatos);


        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Log.d("Error", listados.toString());
                Datos datos = arraydatos.get(position);
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);


                // intent.putExtra("id", lista.getItemAtPosition(position).toString());

                intent.putExtra("id", datos.getId());
                intent.getParcelableExtra("foto", datos.getFoto());
                intent.putExtra("nombre", datos.getNombre());
                intent.putExtra("info", datos.getInfo());
                startActivity(intent);
            }
        });




        //Lo aplico
        lista.setAdapter(adapter);
    }
}

File - SecondActivity.java

public class SecondActivity extends AppCompatActivity {
        //private Toolbar mToolbar;
        private TextView textView;
        private ImageView flag;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);

            //Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar1);
            ImageView flag = (ImageView) findViewById(R.id.imageView_fotoMostrar);
            TextView textView = (TextView)findViewById(R.id.textView);


            Bundle bundle = getIntent().getExtras();
            if(bundle != null){

                textView.setText(bundle.getString("nombre"));
                flag.setImageBitmap((Bitmap) bundle.getParcelable("foto"));



    /*
                mToolbar.setTitle(bundle.getString("id"));
                if(mToolbar.getTitle().toString().equalsIgnoreCase("1")){
                    flag.setImageDrawable(getResources().getDrawable(R.drawable.foto1));
                   // flag.setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.foto1));
                }
                */
            }


        }
    }

File - Data.java

public class Datos {
    //Declaramos los siguientes atributos
    protected Drawable foto;
    protected String nombre;
    protected String info;
    protected long id;

    //Método constructor de la clase
    public Datos(Drawable foto, String nombre, String info, long id){
        this.foto = foto;
        this.nombre = nombre;
        this.info = info;
        this.id = id;
    }


    // Métodos get and set

    //Método get obtiene datos
    public Drawable getFoto() {
        return foto;
    }

    //Método set asigna o inicializa los datos
    public void setFoto(Drawable foto){
        this.foto = foto;
    }

    public String getNombre(){
        return nombre;
    }
    public void setNombre(String nombre){
        this.nombre = nombre;
    }
    public String getInfo(){
        return info;
    }
    public void setInfo(String info){
        this.info = info;
    }
    public long getId(){
        return id;
    }
    public void setId(long id){
        this.id = id;
    }
}
  

AdapterDatos.class

public class AdapterDatos extends BaseAdapter {

    //Declaramos los siguiente
    protected Activity activity;
    //Dentro del ArrayList colocamos la clase Datos
    protected ArrayList<Datos> items;

    //Metodo contructor de la clase
    public AdapterDatos(Activity activity, ArrayList<Datos> items){
        this.activity = activity;
        this.items = items;
    }


    @Override
    public int getCount() {
        //Obtenemos el tamaño de los items
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        //Obtenemos la posicion del items
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return items.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Generamos una converView por motivos de eficiencia
        View v = convertView;

        if(convertView == null){
            LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.itemlista, null);
        }

        //Creamos un objecto de la clase Datos
        Datos datos = items.get(position);

            //Rellenamos la fotografia
            ImageView foto = (ImageView) v.findViewById(R.id.imageView_Foto);
            foto.setImageDrawable(datos.getFoto());


            //Rellenamos el nombre
            TextView nombre = (TextView)v.findViewById(R.id.textView_Nombre);
            nombre.setText(datos.getNombre());

            //Rellenamos la info
            TextView info = (TextView)v.findViewById(R.id.textView_Info);
            info.setText(datos.getInfo());

        //Retornamos la vista
        return v;
    }
}
    
asked by Fumatamax 01.08.2018 в 09:34
source

1 answer

0

The first thing I would do is change the variable:

protected Drawable foto;

A string type and there save the name of the photo:

protected String foto;

With this at the time of going to the intent information you must change the way to pass the photo to this:

intent.putExtra("foto", datos.getFoto()); //datos.getFoto() es el nombre de la foto

Now in SecondActivity you must create a variable String to pick up the name of the photo:

String fotoNombre = bundle.getString("foto");

Once the name is obtained, the next step is to get the

  

"resourceId"

of the photo, it would be the unique identifier, and this would be done:

int resID = getResources().getIdentifier(fotoNombre , "drawable", getPackageName());

The ImageView object has a method to assign an image to it by passing it its unique identifier

  

setImageResource (int redId)

The last one would be to assign the resource flag:

flag.setImageResource(resID);

In Adpter class in the constructor you will have to pass the context of the main activity:

protected ArrayList<Datos> items;
protected Context contexto;
    //Metodo contructor de la clase
    public AdapterDatos(Activity activity, ArrayList<Datos> items,Context contexto){
        this.activity = activity;
        this.items = items;
        this.contexto = contexto;
    }

And in the getView method:

Datos datos = items.get(position);

    //Rellenamos la fotografia
    ImageView foto = (ImageView) v.findViewById(R.id.imageView_Foto);
    String fotoNombre = bundle.getString("foto");
    int resID = contexto.getResources().getIdentifier(fotoNombre , "drawable", getPackageName());
    foto.setImageResource(resID);
    
answered by 01.08.2018 в 09:49