嗨,我是Android改装框架的新手。我可以使用它从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;
}
Upload.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));
}?>
此代码对黑屏没有作用
答案 0 :(得分:0)
您正在将空白ArrayList发送到适配器
在这里查看您在onCreate()中分配mUploads = new ArrayList <>();
然后您只需将mUploads传递给mAdapter = new ImageAdapter(Product.this,mUploads);
来自的代码
int32_t
到
@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);
}
答案 1 :(得分:0)
从您的端点我收到以下答复
{"response":"ok","img":"greypant.jpg","name":"GreyPant","id":"1","description":"A very fine pant with great finishing.","price":"1500"}
您可以尝试对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"=> "http://yourdomain.com/{$image}",
"name"=>$name,
"id"=>$id,
"description"=>$des,
"price"=>$price));
}
?>
将http://yourdomain.com/替换为保存图像的目录的完整路径。祝你好运!