【问题标题】:Set ParseFile images in a ListView using custom ArrayList<MyOwnParseClass> in a custom Adapter在自定义适配器中使用自定义 ArrayList<MyOwnParseClass> 在 ListView 中设置 ParseFile 图像
【发布时间】:2014-10-31 02:04:00
【问题描述】:

我正在使用 ListView 来显示解析中的所有记录。使用一个名为 ListadoObjetoParse 的自定义类,它包含一些属性,如 nameaddressParseFile,最后一个包含记录的图像。包含所有内容的数据对象是一个名为 datos 的 ListadoObjetoParse 的 ArrayList。当尝试从名为“datos”的对象获取图像 (ParseFile) 并填充到 listView 中时,问题出现在自定义适配器 (RestaurantesAdapter) 中。无法将图像作为 ParseFile 获取并转换为位图以显示在 listView 中。我只看到了一种使用 GetDAtaInBackGround 方法的方法。下面我展示了 CustomAdapter 和 Class ListadoObjetoParse。

我很感激任何帮助,因为我被困了很长时间。非常感谢

/自定义适配器/

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/

公共类 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;
}

}

【问题讨论】:

    标签: android listview parse-platform adapter listviewitem


    【解决方案1】:

    第一 尝试使用 ParseImageView 代替图像视图并加载 ParseFiles 本机:

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

    如果您不需要回调,则只需 imageView.loadInBackground()

    更多parse doc

    第二

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

    【讨论】:

      【解决方案2】:

      感谢您的建议,我可以修复它。我一开始无法获得它,但结合 ParseImagView 和对 AdapterView 工作原理的一些理解可以让它工作。下面是代码解释:

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          // Inflate The view to show on the ListView Object
          View item = LayoutInflater.from(context).inflate(
                  R.layout.mostrar_listado_detalle_restaurante, null);
      
          //Instanciate of ParseImageView that will content the image ParseFile
          ParseImageView foto = (ParseImageView) item.findViewById(R.id.imgRestaurante);
      
         //This gets the result of a search of restaurants in a previous activity
          List<ParseObject> res = BusquedaRestaurante.ObtenerObjectoParse.getObjeto();
      
          //We set ParseObject (Restaurant) of the List using position var
          ParseObject resf = res.get(position);
      
          //recover the "picture" of that restaurant
          ParseFile file = resf.getParseFile("Foto");
      
          //Here we check if there is a picture 
          if (file != null) {
              foto.setParseFile(file);
              foto.loadInBackground();
              Log.d("ERROR:", "Picture is set in ParseImageView");
          } else {
      
               Log.d("ERROR:", "There is no picture in the Restaurant photo field");
          }
      
      
          // Other
      
      
          return item;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        相关资源
        最近更新 更多