gpt4 book ai didi

android - 如何从 RecyclerView 保存更改的数据并上传更改

转载 作者:行者123 更新时间:2023-11-29 02:25:50 25 4
gpt4 key购买 nike

我正在处理一张图像 并从中获取“数据”。然后我在 RecyclerView 中显示它们。如有必要,我会更正 RecyclerView 中的一些数据。如何将更正后的数据与其他所有内容一起上传,因为即使 RecyclerView 中显示更正后也没有上传更改,而是上传了错误的数据。有人可以帮助我被困了几天......

这是我要将数据添加到 Recyclerview 的 mainActivity 的一部分?

TextExtractionActivity.class

if (fileDirectory.isDirectory()) {
listCroppedImages.clear();
EmptyViewCroppedImage.setVisibility(View.GONE);
RVCroppedImages.setVisibility(View.VISIBLE);
listCroppedImages.clear();
String PhotoPath[] = new String[100];
final String StudentMatric[] = new String[100];
final String AttendanceRecord[] = new String[100];

for (int i = 1; i <= fileDirectory.listFiles().length; i++) {
PhotoPath[i] = croppedImageDirectory + i + ".jpg";

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap croppedimageold = BitmapFactory.decodeFile(PhotoPath[i], options);
Bitmap croppedimagenew = Bitmap.createScaledBitmap(croppedimageold, 460, 66, true);

StudentMatric[i] = TextImageProcess(croppedimagenew);
AttendanceRecord[i] = CircleDetection(croppedimagenew, StudentMatric[i]);
listCroppedImages.add(new CroppedImageModel(String.valueOf(i), PhotoPath[i], StudentMatric[i], AttendanceRecord[i]));

btnUploadAttendance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


for (int x = 1; x <= listCroppedImages.size(); x++) {
UploadData(StudentMatric[x], AttendanceRecord[x], x);
}
}
});


}
} else {
EmptyViewCroppedImage.setVisibility(View.VISIBLE);
RVCroppedImages.setVisibility(View.GONE);
}

-----------------------------------------------------------------------

public void UploadData(final String StudentMatric, final String AttendanceRecord, final int x) {
ProgressUploadAttendance.setVisibility(View.VISIBLE);

Query query = StudentsRef.orderByKey().equalTo(StudentMatric);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
int progress = x / listCroppedImages.size() * 100;
DatabaseReference StudentMatricRef = StudentsRef.child(StudentMatric).child("Attendance").push();
StudentMatricRef.child("Status").setValue(AttendanceRecord);
StudentMatricRef.child("Date").setValue(getCurrentDate());
ProgressUploadAttendance.setProgress(progress);
} else {
Toast.makeText(TextExtractionActivity.this, "Could not Find " + StudentMatric, Toast.LENGTH_LONG).show();
}

}

@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});

}

“RecyclerViewAdapterCroppedImages.class”

class RecyclerViewAdapterCroppedImages extends RecyclerView.Adapter<RecyclerViewAdapterCroppedImages.CroppedimageViewHolder> {

private Context mContext;
private List<CroppedImageModel> mData;
int x =0;

// private String StudentMatric, studentMatric, AttendanceStatus, attendanceStatus;

public RecyclerViewAdapterCroppedImages() { } //Constructor


public RecyclerViewAdapterCroppedImages(Context mContext, List<CroppedImageModel> mData) {
this.mContext = mContext;
this.mData = mData;
}


@Override
public CroppedimageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View croppedimageview;
LayoutInflater mInflater = LayoutInflater.from(mContext);
croppedimageview = mInflater.inflate(R.layout.item_cropped_image, parent, false);
return new CroppedimageViewHolder(croppedimageview);
}

@Override
public void onBindViewHolder(final CroppedimageViewHolder holder, final int position) {

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap croppedimageold = BitmapFactory.decodeFile(mData.get(position).getCroppedImagePath(), options);
Bitmap croppedimagenew = Bitmap.createScaledBitmap(croppedimageold, 528, 80, true);


holder.StudentNo.setText(mData.get(position).getStudentNo());
holder.CroppedImage.setImageBitmap(croppedimagenew);
holder.StudentId.setText(mData.get(position).getStudentMatric());
holder.StudentStatus.setText(mData.get(position).getAttendanceRecord());

holder.StudentStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mData.get(position).getAttendanceRecord().equals("Failed")) {
holder.StudentStatus.setText("Present");
} else if (mData.get(position).getAttendanceRecord().equals("Present")) {
holder.StudentStatus.setText("Absent");
} else if (mData.get(position).getAttendanceRecord().equals("Absent")) {
holder.StudentStatus.setText("Present");
}
}
});


}

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


class CroppedimageViewHolder extends RecyclerView.ViewHolder {
TextView StudentNo, StudentId, StudentStatus;
ImageView CroppedImage;

private CroppedimageViewHolder(View itemView) {
super(itemView);
StudentNo = itemView.findViewById(R.id.tvtxtprocessstudentno);
CroppedImage = itemView.findViewById(R.id.ivcroppedimage);
StudentId = itemView.findViewById(R.id.tvtxtprocessstudentid);
StudentStatus = itemView.findViewById(R.id.tvtxtprocessstudentstatus);
}
}

}

最佳答案

我找到了解决方案。问题是我认为我必须通过位于我的主要 Activity 中的适配器通知 notifiyOnDatachange。但我不必..

在我的 RecyclerViewAdapterCroppedImage.class 中

final CroppedImageModel changedRecord = mData.get(position);
holder.IVattendance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

switch (mData.get(position).getAttendanceRecord())
{
case "Failed":
mData.get(position).setAttendanceRecord("Present");
correctionAttendance(position, changedRecord);//code
break;

case "Present":
mData.get(position).setAttendanceRecord("Absent");
correctionAttendance(position, changedRecord);
break;

case "Absent":
mData.get(position).setAttendanceRecord("Present");
correctionAttendance(position, changedRecord);
break;

default:
break;
}
}
});

public void correctionAttendance(int position, CroppedImageModel attendanceRecord ) {
mData.set(position, attendanceRecord);
notifyItemChanged(position);
notifyDataSetChanged();
}

在我的 TextExtractionActivity.class 中,我直接从 Adapter 获取数据,而不是在我处理数据并将数据发送到 Adapter 的地方。

btnUploadAttendance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for (int x = 0; x < listCroppedImages.size(); x++) {
UploadData(listCroppedImages.get(x).getStudentMatric(),listCroppedImages.get(x).getAttendanceRecord(),x);
}
}
});

最后我的 UploadData() 与问题中所述保持相同..感谢任何打算回答或提供格式更正的人..

关于android - 如何从 RecyclerView 保存更改的数据并上传更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com