我想在recyclerview中设置图像,并从mysql数据库中设置图像。我已尝试使用imageView.setImageResource(motorType.getImage)
但不能因为 setImageResource 使用int,而我使用 / files / image-f875803a-164f-11e8-b31a等字符串将我的图像保存在数据库中-005056b25997.png
那么如何在适配器中设置我的Image?三江源..
这是我的型号代码(ImageMotor.java)
public class ImageMotor {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("image")
@Expose
private String image;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
CatalogActivity.java
private void refreshingData() {
API.getImageMotor().enqueue(new Callback<ArrayList<ImageMotor>>() {
@Override
public void onResponse(Call<ArrayList<ImageMotor>> call, Response<ArrayList<ImageMotor>> response) {
datas = response.body();
tempDatas = datas;
recyclerView.setAdapter(new TypeMotorAdapter(datas));
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call<ArrayList<ImageMotor>> call, Throwable throwable) {
Toast.makeText(CatalogActivity.this,"Data Kosong", Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
}
});
}
public class TypeMotorAdapter extends RecyclerView.Adapter<TypeMotorAdapter.TypeMotorViewHolder> {
List<ImageMotor> dataSet;
public TypeMotorAdapter(List<ImageMotor> data) {
this.dataSet = data;
}
@Override
public TypeMotorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_catalog_item, parent, false);
return new TypeMotorViewHolder(itemView);
}
@Override
public void onBindViewHolder(TypeMotorViewHolder holder, int position) {
ImageMotor motorType = dataSet.get(position);
holder.imageView.setImageResource(motorType.getImage());
holder.tv_motor.setText(motorType.getName());
}
@Override
public int getItemCount() {
if(dataSet == null){
return 0;
}else{
return dataSet.size();
}
}
public class TypeMotorViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView tv_motor;
public TypeMotorViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image_type_motor);
tv_motor = (TextView) itemView.findViewById(R.id.tv_motor);
}
}
}
答案 0 :(得分:2)
如果要在ImageView中显示图像并以字符串格式存储在mysql中,请按照以下步骤操作。我使用滑动库来显示负载图像数据,使用recyclelerview来显示输入。
implementation 'com.github.bumptech.glide:glide:4.6.1' library or picasso.
<强>活动强>
public static String imagePath;
//Request Camera Permission for User
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case Myutility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (userChoosenTask.equals("Choose from Library"))
galleryIntent();
}
}
//Intent For Gallary
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);//
startActivityForResult(Intent.createChooser(intent, "Select File"), RESULT_LOAD_IMG);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
String picturePath = "";
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
if (selectedImage == null) {
selectedImage = Uri.EMPTY;
}
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
Glide.with(this)
.load(picturePath)
.error(ContextCompat.getDrawable(this, R.drawable.error))
.placeholder(R.drawable.error)
.centerCrop()
.into(imageView);
cursor.close();
}
Bitmap bitmap;
int width = imageView.getWidth();
int height = imageView.getHeight();
bitmap = BitmapFactory.decodeFile(picturePath);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
imageView.setImageBitmap(bitmap);
imagePath = picturePath;
} else if (resultCode == RESULT_CANCELED && null != data) {
Toast.makeText(this, R.string.cancelevent, Toast.LENGTH_SHORT).show();
}
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageViewAddImage:
onPickPhoto();
break;
//image select click
case R.id.imageView:
final CharSequence[] items = {getString(R.string.chooseForLibrary),
getString(R.string.cancel)};
AlertDialog.Builder builder = new AlertDialog.Builder(AddReferenceActivity.this);
builder.setTitle(R.string.titleAddPhoto);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = Myutility.checkPermission(AddReferenceActivity.this);
if (items[item].equals("Choose from Library")) {
userChoosenTask = "Choose from Library";
if (result)
galleryIntent();
} else if (items[item].equals(getString(R.string.userclickCancel))) {
dialog.dismiss();
}
}
});
builder.show();
default:
break;
ADAPTER CLASS
public class AddReferenceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<PersonFeed> personArray = new ArrayList<>();
public AddReferenceAdapter(Context context, ArrayList<PersonFeed> arrayFeeds) {
this.mContext = context;
this.personArray = arrayFeeds;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.carditemview, parent, false);
return new ViewHolder(view); // Change The Return Value
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holderMain, final int position) {
final ViewHolder holder = (ViewHolder) holderMain;
PersonFeed feedInfoObject = personArray.get(position);
Glide.with(mContext)
.load(feedInfoObject.getUrl())
.error(ContextCompat.getDrawable(mContext, R.drawable.maan))
.bitmapTransform(new RoundedCornersTransformation(mContext, 50, 0))
.fitCenter()
.placeholder(R.drawable.maan)
.crossFade(30)
.into(holder.ImageView);
holder.textviewPersonName.setText(feedInfoObject.getPersonName());
holder.textviewpersionDesignation.setText(feedInfoObject.getPersondesignation());
holder.textviewPersonDetail.setText(feedInfoObject.getPersonDetail());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mContext, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
//delete items on recyclerview event
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
removeitem(position);
notifyDataSetChanged();
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
//remove items and set tip message for user
private void removeitem(int position) {
personArray.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, personArray.size());
if (personArray.isEmpty()) {
((MainActivity) mContext).Nodaatafound.setVisibility(View.VISIBLE);
((MainActivity) mContext).recyclerView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return personArray.size();
}
private class ViewHolder extends RecyclerView.ViewHolder {
//declar controls
private final View myView;
private final ImageView ImageView;
private final TextView textviewPersonName;
private TextView textviewpersionDesignation;
private TextView textviewPersonDetail;
private TextView buttonViewOption;
private ViewHolder(View itemView) {
super(itemView);
myView = itemView;
textviewPersonName = myView.findViewById(R.id.textviewPersonName);
textviewpersionDesignation = myView.findViewById(R.id.textviewpersionDesignation);
textviewPersonDetail = myView.findViewById(R.id.textviewPersonDetail);
ImageView = myView.findViewById(R.id.cardimage);
buttonViewOption = myView.findViewById(R.id.textViewOptions);
//note : if your layout is linear then set Linearlayout objects.
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ImageView.getLayoutParams();
//layoutParams.height = dataToSet;
ImageView.setLayoutParams(layoutParams);
}
}
}
不要忘记在清单文件中设置相机,互联网或存储权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
为此目的尝试Glide:
Glide.with(this).load(motorType.getImage()).into(holder.imageView);
在gradle中:
compile 'com.github.bumptech.glide:glide:4.6.1'
毕加索使用:
Picasso.with(this).load(motorType.getImage()).into(holder.imageView);