我有一个应用程序,我捕获图像并将其与DATE的名称一起保存到firebase实时数据库。
我从我的MainActivity中检索我的图像名称(日期)到另一个活动的Recyclerview。
当我在MainActivity中调用相机时,它会在捕获时自动上传文件。这是上传图片的代码:
private void uploadFile() {
final String bothStamp = new SimpleDateFormat("yyyy.MM.dd HH mm ss").format(new Date());
final String dateStamp = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
final String timeStamp = new SimpleDateFormat("HH mm ss").format(new Date());
if (mImageUri != null) {
StorageReference fileReference = mStorageRef.child(bothStamp
+ "." + getFileExtension(mImageUri));
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 500);
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
Upload upload = new Upload(
taskSnapshot.getDownloadUrl().toString(), dateStamp, timeStamp );
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(upload);
然后我有我的UPLOAD课程:
public class Upload {
private String mTime;
private String mName;
private String mImageUrl;
public Upload() {
//empty constructor
}
public Upload(String imageUrl, String name, String time) {
mImageUrl = imageUrl;
mName = name;
mTime = time;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
然后我有了适用于recyclerview的ADAPTER:
public class DateListAdapter extends RecyclerView.Adapter<DateListAdapter.ImageViewHolder> {
private Context mContext;
private List<Upload> mUploads;
public DateListAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUploads = uploads;
}
@Override
public DateListAdapter.ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.date_list, parent, false);
return new DateListAdapter.ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
String dateName = uploadCurrent.getName();
holder.textViewName.setText(dateName);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.text_view_name_date);
}
}
}
现在,当我在一天内拍摄多张图片时,这是我的输出:
This is what my code output is right now in the user interface
这就是我想要的OUTPUT:
This is what I would like to show in my RECYCLERVIEW
所以我的目的是创建一个CLEAN和简单的USER界面。我不希望显示多个日期,我只想要一个日期,然后当用户点击日期时,它应该显示在该日期捕获的所有图像。
这是我的RecyclerView活动中的EventListener:
mDatabaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new DateListAdapter(CapturedImageDateListActivity.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
答案 0 :(得分:1)
由于日期相同,您的数据在存储时会被覆盖。 您所要做的就是根据当前日期存储数据。 现在您可以根据不同的日期轻松获取数据。!
final String dateStamp = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
ImageModel imageValue=new ImageModel(datestamp,imageUrl);
mdatabaseReference.child("imageGalleryNode").child(dateStamp).child(getRandomId()).setValue(imageValue)
如果您想维护每个用户数据。
mdatabaseReference.child("imageGalleryNode").child(dateStamp).child(mAuth.getCurrentUser().getUid()).child(getRandomId()).setValue(imageValue)
public String getRandomId() {
String id = UUID.randomUUID().toString();
return id;
}
使用firebase事件监听器获取数据。!
ArrayList<String> dateNodeKeyList=new Arraylist<>();
databaseReference = FireBaseHelper.getDataBaseRefrence();
databaseReference.child("imageGalleryNode");
databaseReference .addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dateNodeKeyList.size>0){ //to avoid data duplication on data change.
dateNodeKeyList.clear();
}
ArrayList<ImagesModel> arrayList = new ArrayList<>();
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
// key for node .!
String key = childSnapshot.getKey();
dateNodeKeyList.add(key);
// ImagesModel object =
childSnapshot.getValue(ImagesModel.class);
arrayList.add(object);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
并且您的json
将是这样的。!
{
"26-02-2018": {
"13d49659-8ab1-48b7-808b-987f43de9fbc": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "13d49659-8ab1-48b7-808b-987f43de9fbc",
"imageUrl": 0,
"imageid": "fa7d6135-64ba-4bbe-8b9e-078bc6171463",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
},
"51c51cec-ac6d-4766-823e-df9e27cfc169": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "51c51cec-ac6d-4766-823e-df9e27cfc169",
"imageUrl": 0,
"imageid": "f16185f4-05b6-4d77-8019-c09563e044ce",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
}
},
"27-02-2018": {
"13d49659-8ab1-48b7-808b-987f43de9fbc": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "13d49659-8ab1-48b7-808b-987f43de9fbc",
"imageUrl": 0,
"imageid": "fa7d6135-64ba-4bbe-8b9e-078bc6171463",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
},
"51c51cec-ac6d-4766-823e-df9e27cfc169": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "51c51cec-ac6d-4766-823e-df9e27cfc169",
"imageUrl": 0,
"imageid": "f16185f4-05b6-4d77-8019-c09563e044ce",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
}
},
"28-02-2018": {
"13d49659-8ab1-48b7-808b-987f43de9fbc": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "13d49659-8ab1-48b7-808b-987f43de9fbc",
"imageUrl": 0,
"imageid": "fa7d6135-64ba-4bbe-8b9e-078bc6171463",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
},
"51c51cec-ac6d-4766-823e-df9e27cfc169": {
"DHFNJoa2hPdtM1sapeIFPQpDH7c2": {
"imageName": "51c51cec-ac6d-4766-823e-df9e27cfc169",
"imageUrl": 0,
"imageid": "f16185f4-05b6-4d77-8019-c09563e044ce",
"userId": "DHFNJoa2hPdtM1sapeIFPQpDH7c2"
}
}
}
}