android - 如何使用 Retrofit 2 从数据库中获取图像

标签 android mysql database retrofit2

您好,我是 Android Retrofit 框架的新手。我可以使用它从 REST 服务获取 JSON 响应,但我不知道如何使用 Retrofit2 获取图像。我正在尝试从数据库中获取recyclerview中的图像

代码在这里:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product);
    Intent intent = getIntent();
    parent = intent.getStringExtra("Parent");
    child = intent.getStringExtra("Child");
    Toast.makeText(this, parent+child, Toast.LENGTH_SHORT).show();
    //init firebase
    //load menu
    recycler_menu =(RecyclerView)findViewById(R.id.recycler_view);
    apiInterface = 
    ApiClient.getRetrofit().create(ApiInterface.class);
    recycler_menu.setLayoutManager(new GridLayoutManager(this, 2));
    recycler_menu.setHasFixedSize(true);

    mUploads = new ArrayList<>();
    loadMenu();

   }
     private void loadMenu(){

     Toast.makeText(Product.this, "Hello", 
     Toast.LENGTH_SHORT).show();
     Call<Upload> call = apiInterface.performProduct(parent,child);
     call.enqueue(new Callback<Upload>() {
         @Override
         public void onResponse(Call<Upload> call, Response<Upload> 
        response) {
             Toast.makeText(Product.this, "Hello", Toast.LENGTH_SHORT).show();
             mAdapter = new ImageAdapter(Product.this, mUploads);
             mRecyclerView.setAdapter(mAdapter);
         }
         @Override
         public void onFailure(Call<Upload> call, Throwable t) {

         }
     });
 }}

ImageAdapter.java 。

public ImageAdapter(Context context, List<Upload> uploads) {
    mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getImgName());
    holder.textViewPrice.setText(uploadCurrent.getPrice());
    Picasso.get()
            .load(uploadCurrent.getImgUrl())
            .placeholder(R.mipmap.ic_launcher)
            .fit()
            .centerCrop()
            .into(holder.imageView);
    //holder.collection.setText(uploadCurrent.getmRadioGroup());
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
        View.OnCreateContextMenuListener {
    public TextView textViewName;
    public TextView textViewPrice;
    public ImageView imageView;


    public ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.menu_name);
        textViewPrice = itemView.findViewById(R.id.menu_price);
        imageView = itemView.findViewById(R.id.menu_price);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                mListener.onItemClick(position);
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Do whatever");
        MenuItem delete = menu.add(Menu.NONE, 2, 2, "Delete");
    }

}

public interface OnItemClickListener {
    void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

上传.class

public class Upload {
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@SerializedName("id")
private String id;
@SerializedName("image")
private String imgName;
@SerializedName("imgUrl")
private String imgUrl;
@SerializedName("price")
private String price;
@SerializedName("description")
private String Description;
@SerializedName("response")
private String Response;


public  String getResponse(){
    return Response;
}

public Upload() {
}

public Upload(String id,String imgName, String imgUrl, String price, String Description) {
    this.id = id;
    this.imgName = imgName;
    this.imgUrl = imgUrl;
    this.price = price;
    this.Description = Description;
}

public String getImgName() {
    return imgName;
}

public void setImgName(String imgName) {
    this.imgName = imgName;
}

public String getImgUrl() {
    return imgUrl;
}

public void setImgUrl(String imgUrl) {
    this.imgUrl = imgUrl;
}

public String getDescription() {
    return Description;
}

public void setDescription(String Description) {
    this.Description = Description;
}
public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

}

PHP 文件:

 <?php include ("conn.php");
 $type = $_GET["parent"];
 $ttype = $_GET["child"];
 $sth = $conn->prepare("SELECT * from product where type='$type' && 
 s_type='$ttype'");
 $sth->execute();
 While ($data = $sth->fetch(PDO::FETCH_ASSOC)){
 $name = $data['name']; 
 $id = $data['id'];
 $des = $data['description'];
 $price = $data['price'];
 $image = $data['image'];
 $status = "ok";
 echo 
 json_encode(array("response"=>$status,
 "img"=>$image,"name"=>$name,
 "id"=>$id,"description"=>$des,"price"=>$price));
  }?>

此代码不执行任何操作,出现空白屏幕

最佳答案

You are sending blank ArrayList to Adapter

看这里在 onCreate() 中你分配 mUploads = new ArrayList<>();

然后你只需将 mUploads 传递给 mAdapter = new ImageAdapter(Product.this, mUploads);

更改代码

 @Override
     public void onResponse(Call<Upload> call, Response<Upload> 
    response) {
         Toast.makeText(Product.this, "Hello", Toast.LENGTH_SHORT).show();
         mAdapter = new ImageAdapter(Product.this, mUploads);
         mRecyclerView.setAdapter(mAdapter);
     }

到,

 @Override
     public void onResponse(Call<Upload> call, Response<Upload> 
    response) {
         Toast.makeText(Product.this, "Hello", Toast.LENGTH_SHORT).show();
         mUploads=responese.body();  //@ add this line
         mAdapter = new ImageAdapter(Product.this, mUploads);
         mRecyclerView.setAdapter(mAdapter);
     }

关于android - 如何使用 Retrofit 2 从数据库中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404789/

相关文章:

php - mysql 日期显示 30/11/-000 而不是 0000-00-00

android - 检查设备是否正在运行 cordova

java - 字体会影响TextView的高度吗?

android - 如何在 Visual Studio/Xamarin 项目中将混淆的 .dll 替换为我的 .apk

android - 创建一个内部有 Pojo 的 Pojo?

sql - 有没有更简单的方法来找到具有最大值的行?

php - 将 PHP 插入无法在线工作的 mySQL 数据库(通过 Flash 应用程序)

android - react-native 上的 Realm : can't get results when not debugging

mysql - 使一个表中的记录条目引用另一个表中的多条记录

php - 将oracle转换为mysql以选择listagg并左连接