我已经实施了Firebase Database
,可以成功添加项目,然后在RecyclerView
上显示。我还设法删除了一个成功的数据库的子项,但我需要重新启动活动以查看手机屏幕上的更改。例如:当我在列表项上按Delete时,它会立即从数据库中消失,但我需要重新启动活动以查看更改。这是我的代码片段:
private void attachDatabaseReadListener() {
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
locationCurrent.setRefKey(dataSnapshot.getKey());
mLocationAdapter.add(locationCurrent);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我相信我应该在onChildRemoved
工作,但不知道从哪里开始。我的主要想法是使用for循环重新填充recyclerview
,但是我从datasnapshot获得的locationCurrent对象为null。
我应该从哪里开始寻找解决方案?我还考虑过在我的查询中运行addValueEventListener
方法,但我遇到的问题是我得到了我的单个孩子的多个副本
更新 这里提到的一些评论是我的适配器
public class LocationAdapter extends ArrayAdapter<LocationCurrent> {
public LocationAdapter(Context context, int resource, List<LocationCurrent> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_location, parent, false);
}
LocationCurrent currentLocation = getItem(position);
TextView nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
TextView descriptionTextView = (TextView) convertView.findViewById(R.id.descriptionTextView);
nameTextView.setText(currentLocation.getName());
descriptionTextView.setText(currentLocation.getDescription());
return convertView;
}
}
顺便说一下 - 我在LocationCurrent类中使用的Ref Key是瞬态变量,因此在数据库中不可见。
UPDATE2
经过一整天的工作后,我仍然无法在删除适配器后立即将其取出。相反 - 我提出了一个临时解决方案 - 我在recreate()
内添加了一个onChildRemoved
方法,它确实起了作用。 (不是一个好习惯,但仍然 - 某事)
答案 0 :(得分:1)
基本上,这就是我删除方法的方法。
/**
* This removes the CardView from the RecyclerView, allowing us to create a delete request
*
* http://stackoverflow.com/questions/27293960/swipe-to-dismiss-for-recyclerview/30601554#30601554
*
* @param viewHolder
* @param direction
*/
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// First find the position of the card
// https://www.learn2crack.com/2016/02/custom-swipe-recyclerview.html
int position = viewHolder.getAdapterPosition();
// Connect to the database, identify which card was swiped via the RecyclerViewAdapter
DatabaseReference currRef = adapter.getRef(position);
// Get it out of the DB
currRef.removeValue();
}
以上代码应该让您大致了解删除过程。您需要检索要删除的元素的索引。在您的上下文中,您的位置键以某种方式绑定。
仅供参考,这是基于FirebaseRecyclerAdapter的populateViewHolder。
/**
* Populating the RecyclerView..
*
* @param viewHolder
*
*
* @param task
*
*
* @param position
* With the use of position, we can obtain the key of from the FirebaseDatabase
*
* http://stackoverflow.com/questions/37568703/how-to-get-keys-and-values-using-firebaselistadapter
*/
@Override
protected void populateViewHolder(TaskViewHolder viewHolder, Tasks task, int position) {
// Get the key of the Tasks object
//String currentKey = getRef(position).push().getKey();
//final String currentKey = getRef(position).toString(); // This returns the object URL from Firebase
final String currentKey = getRef(position).getKey();
Log.d(TAG, currentKey.toString());
Log.d(TAG, "Image: " + task.getImageUrl());
// Perform some DateTime formatting from the ISO8601 format
// Basically we need to attach the task to the viewHolder so that
// the cards can instantiate their view properly
viewHolder.setTaskName(task.getTaskName());
viewHolder.setTaskDesc(task.getTaskDescription());
viewHolder.setTaskDate(task.getTaskDeadline());
viewHolder.setTaskImage(task.getImageUrl());
final Intent updateView = new Intent(getActivity(), UpdateTaskActivity.class);
// Implement Serializable on the Tasks object,
// Push the object directly via updateView.putExtra
// That way we can have everything we need in the object.
//updateView.putExtra("TaskName", task.getTaskName());
//updateView.putExtra("TaskDesc", task.getTaskDescription());
updateView.putExtra("TaskObject", task);
updateView.putExtra("Key", currentKey);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* How to provide a foundation to animate cards
*
* http://stackoverflow.com/questions/27300441/how-do-i-expand-cardviews-to-show-more-detail-like-google-keep-cards
*/
//Toast.makeText(getActivity(), currentKey, Toast.LENGTH_LONG).show(); // Test Line to Showcase the Key.
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
v, // The view which starts the transition
getString(R.string.transition_taskcard) // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(getActivity(), updateView, options.toBundle());
}
});
}
只需显示此示例即可让您知道您的视图和删除代码都必须是动态的。
如果您正在进行动态视图,请参阅此处的文档https://github.com/firebase/emberfire
UPDATE 由于您刚刚添加了适配器,因此如果您打算在getView中使用onClickListener也可以执行任何删除。