I am using a ListView to show all records from parse. With a custom Class called ListadoObjetoParse that contains some attributes like name, address and ParseFile, this last one contains an image of the record. Data object that contains everything is an ArrayList of ListadoObjetoParse called datos. The problems comes in the custom Adapter (RestaurantesAdapter) when trying to get images (ParseFile) and populate in the listView from that object named "datos". Its not possible to fetch the image as a ParseFile and convert to Bitmap to show in the listView. I have seen only one way using GetDAtaInBackGround method. Below i show CustomAdapter and Class ListadoObjetoParse.
I'd appreciate any help to show some light with this as i am stucked since long time. Thank you very much
/CUSTOM ADAPTER/
public class RestaurantesAdapter extends ArrayAdapter<ListadoObjetoParse> {
private Context context;
private ArrayList<ListadoObjetoParse> datos;
public RestaurantesAdapter(Context context, ArrayList<ListadoObjetoParse> datos) {
super(context, R.layout.activity_mostrar_listado_restaurantes, datos);
this.context = context;
this.datos = datos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = LayoutInflater.from(context).inflate(
R.layout.mostrar_listado_detalle_restaurante, null);
final ImageView imagen = (ImageView) item.findViewById(R.id.imgRestaurante);
//Here i fetch Image from the class ListadoObjetoParse with method getImagen
ParseFile i = datos.get(position).getImagen();
/*Expected to do this, but of course not possible convert*/
Bitmap i = (Bitmap) datos.get(position).getImagen();
/*Example Way that is shown in tutorials and parse to fetch the image. But doesn't work for my "datos" object*/
i.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
//Convert ParseFile image of item to Bitmap and show in the listItem
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imagen.setImageBitmap(bmp);
} else {
Log.d("test", "There was a problem downloading the data.");
}
}
});
//Setting name of item in listview
TextView nombre = (TextView) item.findViewById(R.id.tvNombre);
nombre.setText(datos.get(position).getNombre());
//Setting phone number of item in listview
TextView telefono = (TextView) item.findViewById(R.id.tvTelefono);
telefono.setText(datos.get(position).getTelefono());
//Setting address of item in listview
TextView direccion = (TextView) item.findViewById(R.id.tvDireccion);
direccion.setText(datos.get(position).getDireccion());
return item;
}
/LISTADOOBJECTOPARSE.CLASS/
public class ListadoObjetoParse {
private String nombre;
private String direccion;
private String ciudad;
private String telefono;
private ParseFile imagen;
//*CONSTRUCT*/
public ListadoObjetoParse() {}
public ListadoObjetoParse(String nombre, String ciudad, String direccion, String telefono){
this.nombre = nombre;
this.ciudad = ciudad;
this.direccion = direccion;
this.telefono = telefono;
}
public ListadoObjetoParse(String nombre, String ciudad, String direccion, String telefono, ParseFile imagen){
this.nombre = nombre;
this.ciudad = ciudad;
this.direccion = direccion;
this.telefono = telefono;
this.imagen = imagen;
}
/*METHODS*/
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getCiudad() {
return ciudad;
}
public void setCiudad(String ciudad) {
this.ciudad = ciudad;
}
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public String getTelefono() {return telefono;}
public void setTelefono(String telefono) {this.telefono = telefono;}
public ParseFile getImagen() {return imagen;}
public void setImagen(ParseFile imagen) {
this.imagen = imagen;
}
}
First Try to use ParseImageView instead Image view and load ParseFiles native:
ParseFile file;
ParseImageView imageView = (ParseImageView) findViewById(android.R.id.icon);
imageView.setParseFile(file);
imageView.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
}
});
or just imageView.loadInBackground()
if you don't need callback.
More parse doc
Second
ParseFile file;
ImageView imageView = (ImageView) findViewById(android.R.id.icon);
byte[] bitmapdata = file.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata.length);
imageView.setImageBitmap(bitmap);