estoy teniendo unos problemas para mostrar una imagen cuando hago click en una lista y paso de actividad. El problema lo tengo creo, en el MainActivity.java en la linea intent.getParcelableExtra("foto", datos.getFoto());
creo que le estoy pasando mal los parametros para que cargue la foto.
Luego lo envió todo a la segunda actividad SecondActivity.java donde intento cargar la foto para mostrar, pero no se carga en ningún momento.
No se que puedo estar haciendo mal para que no muestre la imagen.
Archivo - 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);
}
}
Archivo - 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));
}
*/
}
}
}
Archivo - Datos.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;
}
}