(这是我的适配器)(我想在imageview中显示图像ivReceiverSign图像网址保存在下面显示的签名类中。)
DbAdapter
package com.example.dhruvitpatel.deviceregistration.adapter;
public class DbAdapter extends RecyclerView.Adapter<DbAdapter.ViewHolder> {
List<RegisterDevice> registerDevice;
Context mCtx;
String rowAdded;
public DbAdapter(List<RegisterDevice> registerDevice, Context mCtx) {
this.registerDevice = registerDevice;
this.mCtx = mCtx;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.rowlayout, parent, false);
return new DbAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final DbAdapter.ViewHolder holder, final int position) {
final RegisterDevice registerdetails = registerDevice.get(position);
holder.registerDate.setText("Register Date: " + registerdetails.getOutDate() + registerdetails.getOutTime());
holder.EmpName.setText("Name: " + registerdetails.getEmpName());
holder.Cable.setText("Cable: " + registerdetails.getCableName());
holder.Device.setText("Device: " + registerdetails.getDevName());
holder.ivUserSign.setImageURI(Uri.parse(registerdetails.getUserSign()));
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mCtx);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage("Are you sure, You want to submit Device?");
alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
holder.checkbox.setChecked(false);
final Dialog submitDialog = new Dialog(mCtx);
submitDialog.setContentView(R.layout.submitdevicedialog);
submitDialog.show();
TextView tvSubmittedTo = (TextView) submitDialog.findViewById(R.id.tvSubmittedTo);
final EditText etReceivedBy = (EditText) submitDialog.findViewById(R.id.etReceivedBy);
Button btnSubmitDevice = (Button) submitDialog.findViewById(R.id.btnSubmitDevice);
Button btnCancelSubmit = (Button) submitDialog.findViewById(R.id.btnCancelSubmit);
btnSubmitDevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String receiver = etReceivedBy.getText().toString();
if (receiver.isEmpty()) {
Toast.makeText(mCtx, "Enter receiver Name", Toast.LENGTH_SHORT).show();
} else {
Calendar cal = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("dd / MM / yyyy ");
String submitDate = mdformat.format(cal.getTime());
DateFormat date = new SimpleDateFormat("hh:mm:ss a");
String submitTime = date.format(cal);
long id = registerdetails.getPid();
DataSource.updateSubmittedDevice(submitDate, submitTime, receiver, id);
Intent in = new Intent(mCtx, Signature.class);
in.putExtra("Id", String.valueOf(id));
Log.d("ID:", String.valueOf(id));
mCtx.startActivity(in);
String recSign = registerdetails.getReceiverSign();
registerdetails.setSubmitDate(submitDate);
registerdetails.setSubmitTime(submitTime);
registerdetails.setReciever(receiver);
registerdetails.setDeviceSubmitted(true);
registerdetails.setReceiverSign(recSign);
registerDevice.remove(position);
registerDevice.add(position, registerdetails);
notifyItemChanged(position);
holder.checkbox.setVisibility(View.GONE);
if (registerdetails.getDeviceSubmitted().equals(Boolean.TRUE)) {
String temp = registerdetails.getDevName();
String temp1 = registerdetails.getCableName();
DataSource.updateDataonLoad(temp, temp1);
}
}
}
});
btnCancelSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitDialog.dismiss();
}
});
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
holder.checkbox.setChecked(false);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
if (registerdetails.getDeviceSubmitted().equals(Boolean.TRUE)) {
// if(!registerdetails.getReceiverSign().isEmpty()){
// holder.ivReceiverSign.setImageURI(Uri.parse(registerdetails.getReceiverSign()));
// }
holder.card_view.setCardBackgroundColor(Color.GRAY);
holder.checkbox.setVisibility(View.GONE);
holder.tvSubmit.setVisibility(View.VISIBLE);
holder.tvReceiver.setVisibility(View.VISIBLE);
holder.submitDate.setVisibility(View.VISIBLE);
holder.tvReceiver.setText(registerdetails.getReciever());
holder.submitDate.setText("Submit Date: " + registerdetails.getSubmitDate() + registerdetails.getSubmitTime());
} else {
holder.checkbox.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
return registerDevice.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.registerDate)
TextView registerDate;
@BindView(R.id.EmpName)
TextView EmpName;
@BindView(R.id.Device)
TextView Device;
@BindView(R.id.Cable)
TextView Cable;
@BindView(R.id.checkbox)
CheckBox checkbox;
@BindView(R.id.tvReceiver)
TextView tvReceiver;
@BindView(R.id.tvSubmit)
TextView tvSubmit;
@BindView(R.id.submitDate)
TextView submitDate;
@BindView(R.id.card_view)
CardView card_view;
@BindView(R.id.ivUserSign)
ImageView ivUserSign;
@BindView(R.id.ivReceiverSign)
ImageView ivReceiverSign;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
Register class
(Here I register devices which are loaded in spinner from database.After selecting items i will click on register button and all details will be notified to adapter )
Register Page
package com.example.dhruvitpatel.deviceregistration.activity;
public class TakeDevice extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.spnName)
Spinner spnName;
@BindView(R.id.spnDevice)
Spinner spnDevice;
@BindView(R.id.spnCable)
Spinner spnCable;
@BindView(R.id.btnRegister)
Button btnRegister;
@BindView(R.id.col)
CoordinatorLayout col;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_device);
ButterKnife.bind(this);
initView();
setListener();
loadempData();
loaddevdata();
loadcabledata();
}
private void initView() {
spnName.requestFocus();
}
private void setListener() {
btnRegister.setOnClickListener(this);
}
private void loadempData() {
final List<EmployeeDetails> values = DataSource.getempData();
adapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, values);
adapter.add("Select your name");
spnName.setAdapter(adapter);
spnName.setSelection(adapter.getCount()-1);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnRegister:
empName = spnName.getSelectedItem().toString();
devName = spnDevice.getSelectedItem().toString();
Cable = spnCable.getSelectedItem().toString();
if (empName.isEmpty() && devName.isEmpty() && Cable.isEmpty()) {
Snackbar snack = Snackbar.make(col, "Select details", Snackbar.LENGTH_SHORT);
snack.show();
}
if (Cable.equals("Select cable")){
Snackbar snack = Snackbar.make(col, "Select cable", Snackbar.LENGTH_SHORT);
snack.show();
}
else if(empName.equals("Select your name")){
Snackbar snack = Snackbar.make(col, "Select name", Snackbar.LENGTH_SHORT);
snack.show();
}
else if (devName.equals("Select device")){
Snackbar snack = Snackbar.make(col, "Select device", Snackbar.LENGTH_SHORT);
snack.show();
}
else if (empName.isEmpty()) {
Snackbar snack = Snackbar.make(col, "Select name", Snackbar.LENGTH_SHORT);
snack.show();
} else if (devName.isEmpty() && Cable.isEmpty()) {
Snackbar snack = Snackbar.make(col, "Select device or cable", Snackbar.LENGTH_SHORT);
snack.show();
} else {
Calendar cal = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("dd / MM / yyyy ");
String strDate = mdformat.format(cal.getTime());
DateFormat date = new SimpleDateFormat("hh:mm:ss a");
String strTime = date.format(cal);
deviceTaken = new RegisterDevice();
deviceTaken.setOutDate(strDate);
deviceTaken.setOutTime(strTime);
deviceTaken.setEmpName(empName);
deviceTaken.setDevName(devName);
deviceTaken.setCableName(Cable);
DataSource.insertDetails(deviceTaken);
long regDevList = deviceTaken.getPid();
DataSource.updateBoolean(deviceIdDisp, cableIdDisp);
Intent intent = new Intent(this, Signature.class);
intent.putExtra("RowId", String.valueOf(regDevList));
startActivity(intent);
}
break;
}
(I get my image url in this class.i use this to get signature and i have four buttons in it)
Signature class
package com.example.dhruvitpatel.deviceregistration.activity;
public class Signature extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.btnSave)
Button btnSave;
@BindView(R.id.signLayout)
LinearLayout signLayout;
@BindView(R.id.btnRedo)
Button btnRedo;
@BindView(R.id.btnUndo)
Button btnUndo;
@BindView(R.id.btnClear)
Button btnClear;
@BindView(R.id.cordlayout)
CoordinatorLayout cordlayout;
private static final int GALLERY_REQUEST = 111;
public static String tempDir;
private String uniqueId;
File mypath;
public String current = null;
View mView;
signature mSignature;
Uri signImage;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private Path mPath;
long id;
public boolean cc = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signature);
ButterKnife.bind(this);
initView();
setListener();
}
private void initView() {
btnSave.setEnabled(false);
tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/";
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE);
uniqueId = getTodayDate() + "_" + getCurrentTime() + "_" + Math.random();
current = uniqueId + ".jpeg";
mypath = new File(directory, current);
Log.d("path:", String.valueOf(mypath));
mSignature = new signature(this, null);
mSignature.setBackgroundColor(Color.BLACK);
signLayout.addView(mSignature, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mView = signLayout;
btnRedo.setEnabled(false);
}
private String getTodayDate() {
final Calendar c = Calendar.getInstance();
int todaysDate = (c.get(Calendar.YEAR) * 10000) +
((c.get(Calendar.MONTH) + 1) * 100) +
(c.get(Calendar.DAY_OF_MONTH));
Log.w("DATE:", String.valueOf(todaysDate));
return (String.valueOf(todaysDate));
}
private String getCurrentTime() {
final Calendar c = Calendar.getInstance();
int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) +
(c.get(Calendar.MINUTE) * 100) +
(c.get(Calendar.SECOND));
Log.w("TIME:", String.valueOf(currentTime));
return (String.valueOf(currentTime));
}
private void setListener() {
btnSave.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnUndo.setOnClickListener(this);
btnRedo.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
// Log.v("log_tag", "Panel Saved");
if (!CommonUtils.checkPermission(Signature.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
CommonUtils.requestPermission(Signature.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, GALLERY_REQUEST);
} else {
mView.setDrawingCacheEnabled(true);
mSignature.save(mView);
}
case R.id.btnClear:
mSignature.clear();
// mPath=new Path();
// mSignature.invalidate();
btnSave.setEnabled(false);
break;
case R.id.btnUndo:
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
mSignature.invalidate();
btnRedo.setEnabled(true);
}
break;
case R.id.btnRedo:
if (undonePaths.size() > 0) {
paths.add(undonePaths.remove(undonePaths.size() - 1));
btnRedo.setEnabled(true);
mSignature.invalidate();
}
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case GALLERY_REQUEST:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mView.setDrawingCacheEnabled(true);
mSignature.save(mView);
}
break;
}
}
public class signature extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
Context context;
private Paint mPaint;
private float mX, mY;
private static final float TOLERANCE = 5;
public signature(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;
// we set a new Path
mPath = new Path();
// and we set a new Paint with the desired attributes
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(15f);
}
public void save(View v) {
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(signLayout.getWidth(), signLayout.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(mBitmap);
if (paths.isEmpty() && undonePaths.isEmpty()) {
Snackbar snackbar = Snackbar.make(cordlayout, "Enter your signature", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
try {
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 0, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
String url = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "title:" + current, null);
Log.v("log_tag", "url: " + url);
String rowId = getIntent().getStringExtra("RowId");
if (rowId != null) {
id = Long.parseLong(rowId);
Log.d("id:", String.valueOf(id));
signImage = Uri.fromFile(mypath);
Log.d("uri:", String.valueOf(signImage));
DataSource.updateSign(signImage, id);
}
Intent intent = new Intent(Signature.this, MainActivity.class);
intent.putExtra("RowId", String.valueOf(id));
setResult(1, intent);
startActivity(intent);
Intent in = getIntent();
String submitId = in.getStringExtra("Id");
if (submitId != null) {
long subId = Long.parseLong(submitId);
Uri recsignImage = Uri.fromFile(mypath);
DataSource.updateReceiversign(recsignImage, subId);
}
Intent intent1 = new Intent(Signature.this, MainActivity.class);
intent1.putExtra("ID", String.valueOf(submitId));
setResult(2, intent1);
startActivity(intent1);
//In case you want to delete the file
//boolean deleted = mypath.delete();
//Log.v("log_tag","deleted: " + mypath.toString() + deleted);
//If you want to convert the image to string use base64 converter
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
}
}
// override onSizeChanged
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// your Canvas will draw onto the defined Bitmap
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas = new Canvas(mBitmap);
}
// override onDraw
@Override
protected void onDraw(Canvas canvas) {
if (cc) {
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, 0, 0, clearPaint);
cc = false;
}
// // draw the mPath with the mPaint on the canvas when onDraw
// canvas.drawPath(mPath, mPaint);
else {
for (Path p : paths) {
canvas.drawPath(p, mPaint);
}
canvas.drawPath(mPath, mPaint);
}
}
// when ACTION_DOWN start touch according to the x,y values
private void startTouch(float x, float y) {
undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
// when ACTION_MOVE move touch according to the x,y values
private void moveTouch(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOLERANCE || dy >= TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
// when ACTION_UP stop touch
private void upTouch() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
paths.add(mPath);
mPath = new Path();
}
//override the onTouchEvent
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
btnSave.setEnabled(true);
startTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
btnSave.setEnabled(true);
moveTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
btnSave.setEnabled(true);
upTouch();
invalidate();
break;
}
return true;
}
public void clear() {
paths.clear();
undonePaths.clear();
cc = true;
invalidate();
}
}
}
}
如果我想在我的适配器wid id ivReceiverSign的imageview中设置图像,我会怎么做?
答案 0 :(得分:0)
创建界面
public interface ImageUrl{
void getImageUrl(String imageUrl)
}
在适配器中实现此接口,并使用imageUrl.getImageUrl(imageUrl).
或使用EventBus库。
参考Eventbus:Green robot Event bus