【发布时间】:2020-07-05 08:19:50
【问题描述】:
我在我的适配器中创建了一个负责编辑头像的部分,我正在使用一个名为 android image cropper 的库,它需要在活动结果上运行,但由于我在适配器类中,它不允许我使用活动方法。我搜索了但我无法理解代码,因为大部分使用活动并且我使用片段。这是我的适配器类(只是它的一部分):
holder.btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final LayoutInflater inflater=LayoutInflater.from(context);
View add_view=inflater.inflate(R.layout.dialog_addcontacts,null);
AlertDialog alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setView(add_view);
final TextInputEditText edt_name=add_view.findViewById(R.id.edt_name);
final TextInputEditText edt_phonenumber=add_view.findViewById(R.id.edt_number);
byte[] contactimage=phonebookModel.getImage();
Bitmap bitmap= BitmapFactory.decodeByteArray(contactimage,0,contactimage.length);
final CircleImageView image_profile=add_view.findViewById(R.id.profile_image);
Button btn_add=add_view.findViewById(R.id.btn_add);
btn_add.setText("change");
edt_name.setText(phonebookModel.getName());
edt_phonenumber.setText(phonebookModel.getPhonenumber());
image_profile.setImageBitmap(bitmap);
image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setCropShape(CropImageView.CropShape.OVAL)
.setMaxCropResultSize(2500,2500)
.setAspectRatio(1,1)
.setScaleType(CropImageView.ScaleType.CENTER)
.start(getContext(),ContactsFragment.this);
}
}
});
private byte[] imagetobyte(CircleImageView image){
Bitmap bitmap=((BitmapDrawable) image.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,50,stream);
byte[] bytearray=stream.toByteArray();
return bytearray;
}
这是我的片段:
public class ContactsFragment extends Fragment {
RecyclerView recyclerView;
FloatingActionButton floatingActionButton;
PhoneBookDB phoneBookDB;
CircleImageView imagebutton;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.contactsfragment,container,false);
phoneBookDB =new PhoneBookDB(getContext());
//recycler-contacts
recyclerView= view.findViewById(R.id.recycler_contacts);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
List<PhonebookModel> models=phoneBookDB.getalldata();
Contactsadapter adapter=new Contactsadapter(models,getContext());
recyclerView.setAdapter(adapter);
//add-contacts
floatingActionButton=view.findViewById(R.id.btn_add_contacts);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showdialog();
}
});
return view;
}
public void showdialog() {
final LayoutInflater inflater=LayoutInflater.from(getContext());
View add_view=inflater.inflate(R.layout.dialog_addcontacts,null);
final AlertDialog alertDialog=new AlertDialog.Builder(getContext()).create();
alertDialog.setView(add_view);
final TextInputEditText edt_name=add_view.findViewById(R.id.edt_name);
final TextInputEditText edt_phonenumber=add_view.findViewById(R.id.edt_number);
imagebutton=add_view.findViewById(R.id.profile_image);
imagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setCropShape(CropImageView.CropShape.OVAL)
.setMaxCropResultSize(2500,2500)
.setAspectRatio(1,1)
.setScaleType(CropImageView.ScaleType.CENTER)
.start(getContext(),ContactsFragment.this);
}
});
Button btn_add=add_view.findViewById(R.id.btn_add);
Button btn_cancel=add_view.findViewById(R.id.btn_cancel);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!edt_name.getText().toString().isEmpty()&&!edt_phonenumber.getText().toString().isEmpty()) {
long i= phoneBookDB.insertdata(edt_name.getText().toString(), edt_phonenumber.getText().toString(),imagetobyte(imagebutton));
Toast.makeText(getContext(), i+"", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getContext(), MainActivity.class));
}else {
Toast.makeText(getContext(), "لطفا تمامی فیلد های خواسته شده را پر کنید", Toast.LENGTH_SHORT).show();
}
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
try {
InputStream inputStream= getActivity().getContentResolver().openInputStream(resultUri);
imagebutton.setImageBitmap(BitmapFactory.decodeStream(inputStream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
private byte[] imagetobyte(CircleImageView image){
Bitmap bitmap=((BitmapDrawable) image.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,50,stream);
byte[] bytearray=stream.toByteArray();
return bytearray;
}
【问题讨论】:
标签: java android android-studio android-recyclerview