gpt4 book ai didi

java - 到达时间选择器选择的时间后,如何自动删除我的 firebase 数据库中的整个帖子?

转载 作者:行者123 更新时间:2023-12-04 10:06:23 24 4
gpt4 key购买 nike

我现在遇到的问题是,删除帖子的唯一方法是在帖子到期的确切时间单击帖子。超过有效期没有我点击帖子,帖子将不会被删除。我想要的是在到期时间到达时自动从数据库中删除帖子,而不管用户将进行何种 Activity 。有什么办法可以绕过它吗?下面是我的数据库的快照以及用于将数据从我的数据库检索到我的主页 Activity 和单个帖子 Activity 的代码

Database

HomePage Activity

@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
final String key = "";

Query query = FirebaseDatabase.getInstance().getReference("Posts").limitToLast(50);
FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>().setQuery(query,Post.class).build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options){
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row,parent,false);
return new PostViewHolder(view);
}
@Override
protected void onBindViewHolder(PostViewHolder holder, int position, Post model){
final String post_key = getRef(position).getKey();

holder.setLocation("Location: " + model.getLocation());
holder.set_foodAvailable("Food Available: " + model.getFood_Available());
holder.setImage(model.getImage());
holder.set_foodExpiry("Food Expires At: " + model.getFood_Expiry() + "hrs");
holder.setCounter("Interested Parties: " + model.getCounter());
holder.setUsername("Posted by: " + model.getUsername());

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this,post_key, Toast.LENGTH_SHORT).show();
Intent singlePostIntent = new Intent(HomeActivity.this, SingleActivity.class);
singlePostIntent.putExtra("post_id", post_key);
startActivity(singlePostIntent);
((SimpleItemAnimator) mPostList.getItemAnimator()).setSupportsChangeAnimations(false);
}
});
if(key == post_key){
mDatabaseTest = mDatabase.child(key);
mDatabaseTest.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String expiry = (String) dataSnapshot.child("Food_Expiry").getValue();
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
try {
Date currentDate = format.parse(time);
Date expiryDate = format.parse(expiry);
assert currentDate != null;
if(!currentDate.before(expiryDate) || currentDate.after(expiryDate)){
mDatabaseTest.removeValue();
}
} catch (ParseException e) {
e.printStackTrace();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}
};

adapter.startListening();
mPostList.setAdapter(adapter);
}

Single Post Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single);

mSingleImage = findViewById(R.id.single_image);
mSingleEventName = findViewById(R.id.single_eventName);
mSingleLocation = findViewById(R.id.single_Location);
mSingleMoreDetails = findViewById(R.id.single_moreDetails);
mSingleFoodAvailable = findViewById(R.id.single_foodAvailable);
mSingleDietaryOptions = findViewById(R.id.single_dietaryOptions);
mSingleEstimatedPaxAvailability = findViewById(R.id.single_estimatedPaxAvailability);
mSingleFoodExpiry = findViewById(R.id.single_foodExpiry);
removeBtn = findViewById(R.id.removeButton);
mSingleUsername = findViewById(R.id.single_username);

mCounterView = findViewById(R.id.single_counter);
counterBtn = findViewById(R.id.counterButton);

mDatabaseTest = FirebaseDatabase.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mAuth = FirebaseAuth.getInstance();

mPost_key = getIntent().getExtras().getString("post_id");
//Values that you want to retrieve
mDatabase.child(mPost_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String post_uid = (String) dataSnapshot.child("Uid").getValue();
String post_image = (String) dataSnapshot.child("Image").getValue();
String post_eventName = (String) dataSnapshot.child("Event_Name").getValue();
String post_location = (String) dataSnapshot.child("Location").getValue();
String post_moreDetails = (String) dataSnapshot.child("More_Details_On_Location").getValue();
String post_foodAvailable = (String) dataSnapshot.child("Food_Available").getValue();
String post_username = (String) dataSnapshot.child("Username").getValue();

post_click = (String) dataSnapshot.child("Clicked").getValue();

post_counter = (String) dataSnapshot.child("Counter").getValue();
mCounterView.setText("Interested Parties: " + post_counter);

ArrayList<String> names = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
for(DataSnapshot s : dataSnapshot.child("Dietary_Options").getChildren()){
names.add(s.getValue().toString());
}
Log.d("tag","value" + names.size());
for(String a : names) {
sb.append(a);
sb.append(", ");
}
String str = sb.toString();
str = str.replaceAll(", $", "");
mSingleDietaryOptions.setText("Dietary Options: " + str);

String post_estimatedPaxAvailability = (String) dataSnapshot.child("Estimated_Pax_Availability").getValue();
String post_foodExpiry = (String) dataSnapshot.child("Food_Expiry").getValue();

mSingleEventName.setText("Event Name: " + post_eventName);
mSingleLocation.setText("Location: " + post_location);
mSingleMoreDetails.setText("More Details On Location: " + post_moreDetails);
mSingleFoodAvailable.setText("Food Available: " + post_foodAvailable);
mSingleEstimatedPaxAvailability.setText("Estimated Pax Available: " + post_estimatedPaxAvailability);
mSingleFoodExpiry.setText("Food Expires At: " + post_foodExpiry + "hrs");
mSingleUsername.setText("Posted By: " + post_username);

Picasso.get().load(post_image).into(mSingleImage);

if(mAuth.getCurrentUser().getUid().equals(post_uid)){
removeBtn.setVisibility(View.VISIBLE); //check that only user that post the location can remove the post
}

ArrayList<String> users = new ArrayList<String>();
for(DataSnapshot a : dataSnapshot.child("Users_Interested").getChildren()){
users.add(a.getValue().toString());
}

Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
final String time = simpleDateFormat.format(calendar.getTime());
if(time.equals(post_foodExpiry)){
mDatabase.child(mPost_key).removeValue();
}
//check to see if current user has already clicked the button
if(mAuth.getCurrentUser().getUid().equals(post_click) || users.contains(mAuth.getCurrentUser().getUid())){
counterBtn.setEnabled(false);
counterBtn.setText("Confirmed!");
Log.i("testing",mPost_key);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

counterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});

removeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDatabase.child(mPost_key).removeValue();
Intent mainIntent = new Intent(SingleActivity.this, HomeActivity.class);
startActivity(mainIntent);
}
});
}

最佳答案

@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
String key = "";

Query query = FirebaseDatabase.getInstance().getReference("Posts").limitToLast(50);
FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>().setQuery(query,Post.class).build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options){
@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row,parent,false);
return new PostViewHolder(view);
}
@Override
protected void onBindViewHolder(PostViewHolder holder, int position, Post model){
final String post_key = getRef(position).getKey();


holder.setLocation("Location: " + model.getLocation());
holder.set_foodAvailable("Food Available: " + model.getFood_Available());
holder.setImage(model.getImage());
holder.set_foodExpiry("Food Expires At: " + model.getFood_Expiry() + "hrs");
holder.setCounter("Interested Parties: " + model.getCounter());
holder.setUsername("Posted by: " + model.getUsername());

holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this,post_key, Toast.LENGTH_SHORT).show();
Intent singlePostIntent = new Intent(HomeActivity.this, SingleActivity.class);
singlePostIntent.putExtra("post_id", post_key);
startActivity(singlePostIntent);
((SimpleItemAnimator) mPostList.getItemAnimator()).setSupportsChangeAnimations(false);
}
});

String test = mDatabase.child(post_key).getRef().toString();
mDatabase.child(post_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String expiry = (String) dataSnapshot.child("Food_Expiry").getValue();
//Log.i("expiry",expiry);
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
try {
Date currentDate = format.parse(time);
Date expiryDate = format.parse(expiry);
assert currentDate!= null;
if(!currentDate.before(expiryDate) || currentDate.after(expiryDate)){
mDatabase.child(post_key).getRef().removeValue();
Log.i("remove","please");
}
} catch (ParseException e) {
e.printStackTrace();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
Log.i("test",test);
}
};

adapter.startListening();
mPostList.setAdapter(adapter);
}

关于java - 到达时间选择器选择的时间后,如何自动删除我的 firebase 数据库中的整个帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61568925/

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