我的Recyclerview适配器中有以下代码:
onTagClick
时,上传uploadFiles(...)
文件然后返回URL。addOnCompleteListener
上捕获URL时,updateAnswer(...)
将更新文档内的对象position
。 answer
是使用url更新的对象字段。这里的问题是所有文档都使用相同的更新,而answer
字段也使用相同的URL更新。我试图找到一个解决办法,但似乎无法解决。
public class AdapterNewDetails extends RecyclerView.Adapter<AdapterNewDetails.MyViewHolder> {
CustomClasses customclasses;
FirebaseStorage storage;
FirebaseFirestore db;
private List<GnCCompletedDetails> feedItems;
Boolean uploading = false;
private Context mContext;
String thecurentdocid = "";
public AdapterNewDetails(Context paramContext, List<GnCCompletedDetails> paramList) {
this.mContext = paramContext;
this.feedItems = paramList;
}
public int getItemCount() {
return this.feedItems.size();
}
public void onBindViewHolder(MyViewHolder paramMyViewHolder, int paramInt) {
String str;
GnCCompletedDetails gnCCompletedDetails = this.feedItems.get(paramInt);
TextView textView = paramMyViewHolder.question;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(gnCCompletedDetails.getPosition().toString());
stringBuilder.append(". ");
stringBuilder.append(gnCCompletedDetails.getQuestion().toString());
textView.setText(stringBuilder.toString());
if (gnCCompletedDetails.getType().equals("radio")) {
str = gnCCompletedDetails.getAnswer().toString();
paramMyViewHolder.answer.setText(str);
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} else if (gnCCompletedDetails.getType().equals("dropdown")) {
str = gnCCompletedDetails.getAnswer().toString();
paramMyViewHolder.answer.setText(str);
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} else if (gnCCompletedDetails.getType().equals("text")) {
str = gnCCompletedDetails.getAnswer().toString();
paramMyViewHolder.answer.setText(str);
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} else if (gnCCompletedDetails.getType().equals("gps")) {
try {
JSONObject gpsobj = new JSONObject(gnCCompletedDetails.getAnswer().toString());
String one = "Latitude: " + gpsobj.get("lat").toString();
String two = "<br><br>Longitude: " + gpsobj.get("long").toString();
paramMyViewHolder.answer.setText(Html.fromHtml(one + two));
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (gnCCompletedDetails.getType().equals("checkbox")) {
str = gnCCompletedDetails.getAnswer().toString().replace("[", "").replace("]", "");
paramMyViewHolder.answer.setText(str);
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} else if (gnCCompletedDetails.getType().equals("multi_text")) {
str = gnCCompletedDetails.getAnswer().toString();
this.customclasses.parseMultitestAns(str, paramMyViewHolder.answer);
paramMyViewHolder.answer.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setVisibility(View.GONE);
} else if (gnCCompletedDetails.getType().equals("file_upload")) {
str = gnCCompletedDetails.getAnswer().toString().replace("[", "").replace("]", "");
paramMyViewHolder.answer.setText(str);
ArrayList arrayList = new ArrayList(Arrays.asList((Object[]) str.split(",")));
paramMyViewHolder.answer.setVisibility(View.GONE);
paramMyViewHolder.filesrray.setVisibility(View.VISIBLE);
paramMyViewHolder.filesrray.setTags(arrayList);
paramMyViewHolder.filesrray.setOnTagClickListener(new TagView.OnTagClickListener() {
public void onSelectedTagDrag(int param1Int, String param1String) {
}
public void onTagClick(int param1Int, String param1String) {
if (param1String.trim().toLowerCase().startsWith("http")) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(param1String.trim()));
AdapterNewDetails.this.mContext.startActivity(intent);
} else {
if (!uploading) {
uploadFiles(param1String, gnCCompletedDetails.getPosition().toString(), arrayList, gnCCompletedDetails);
Toasty.info(mContext, "Uploading image, please wait.", Toasty.LENGTH_SHORT).show();
thecurentdocid = gnCCompletedDetails.getDocid().toString();
Log.d("TAG", "uploadFiles: " + gnCCompletedDetails.getDocid().toString());
} else {
Toasty.info(mContext, "File upload already in progress, please wait until it's completed.", Toasty.LENGTH_SHORT).show();
}
}
}
public void onTagCrossClick(int param1Int) {
}
public void onTagLongClick(int param1Int, String param1String) {
}
});
}
paramMyViewHolder.cardview.setOnClickListener(new View.OnClickListener() {
public void onClick(View param1View) {
}
});
}
private void uploadFiles(String param1String, String position, ArrayList arrayList, GnCCompletedDetails item) {
uploading = true;
Log.d("TAG", "uploadFiles: " + param1String);
File file = new File(URI.create(param1String.trim()).getPath());
if (file.exists()) {
StorageReference ref = storage.getReference("insight/surveyimages/");
ref.child(UUID.randomUUID().toString() + ".jpg");
ref.putFile(Uri.parse(param1String.trim())).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
public Task<Uri> then(Task<UploadTask.TaskSnapshot> param1Task) throws Exception {
if (param1Task.isSuccessful()) {
return ref.getDownloadUrl();
}
throw param1Task.getException();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
public void onComplete(Task<Uri> param1Task) {
if (param1Task.isSuccessful()) {
Uri uri = param1Task.getResult();
int thepos = arrayList.indexOf(param1String);
arrayList.set(thepos, uri.toString());
updateAnswer(arrayList, position, item);
}
}
});
} else {
Toasty.warning(mContext, "File does not exist", Toasty.LENGTH_SHORT).show();
}
}
private void updateAnswer(ArrayList arrayList, String position, GnCCompletedDetails item) {
Log.d("TAG", "updateAnswer: " + item.getDocid() + " Position " + position);
if (thecurentdocid.equals(item.getDocid())) {
JSONObject joyce = new JSONObject();
try {
joyce.put("answer", arrayList.toString());
joyce.put("question", item.getQuestion().toString());
joyce.put("position", item.getPosition().toString());
joyce.put("type", item.getType().toString());
Map<String, Object> map = toMap(joyce);
if (customclasses.demo()) {
DocumentReference documentReference = db.collection("insight").document("data").collection("survey_responses_demo").document(item.getDocid().toString());
documentReference.update(position, map).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
uploading = false;
}
});
} else {
DocumentReference documentReference = db.collection("insight").document("data").collection("survey_responses").document(item.getDocid().toString());
documentReference.update(position, map).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
uploading = false;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public MyViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
this.customclasses = new CustomClasses();
storage = FirebaseStorage.getInstance();
db = FirebaseFirestore.getInstance();
return new MyViewHolder(LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.holder_completed_view_answers, paramViewGroup, false));
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView answer;
CardView cardview;
TagContainerLayout filesrray;
public TextView question;
public MyViewHolder(View param1View) {
super(param1View);
this.cardview = (CardView) param1View.findViewById(R.id.cardview);
this.question = (TextView) param1View.findViewById(R.id.question);
this.answer = (TextView) param1View.findViewById(R.id.answer);
this.filesrray = (TagContainerLayout) param1View.findViewById(R.id.filesrray);
}
}
public static Map<String, Object> toMap(JSONObject jsonobj) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keys = jsonobj.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jsonobj.get(key);
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
} else if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
value = toList((JSONArray) value);
} else if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
}