我创建了todoapp,可以在 RecyclerView 中添加任务并删除它。问题是,如果我使用 AlertDialog 点击列表中的任何一项任务,我想一次编辑单个任务。我还想更新 firebase数据库中的编辑任务。
MainActivity.java
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private RecyclerViewAdapter mRecyclerAdapter;
private EditText mTaskEditText;
private DatabaseReference databaseReference;
private List<Task> listOfTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Array list of all tasks
listOfTask = new ArrayList<>();
//get the database reference of FIREBASE
databaseReference = FirebaseDatabase.getInstance().getReference();
//this is where you can enter text of your task
mTaskEditText = (EditText) findViewById(R.id.add_task_box);
//Initializing RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.task_list);
//As we are showing data in Linear form we are using this
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
//When we add text of task, and press Button "Add task" triggers this
Button addTaskButton = (Button) findViewById(R.id.add_task_button);
assert addTaskButton != null;
addTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enteredTask = mTaskEditText.getText().toString();
if(TextUtils.isEmpty(enteredTask)){
Toast.makeText(MainActivity.this, "You must enter a task to Add it",Toast.LENGTH_LONG).show();
return;
}
else{
//If task text is valid Add it in our FIREBASE DATABASE.
Task taskObject = new Task(enteredTask);
databaseReference.push().setValue(taskObject);
mTaskEditText.setText("");
}
}
});
//Database helper methods
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//if added , show all
getAllTask(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//if changed show all
getAllTask(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//delete the task
taskDeletion(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
//On delete, remove the task from Firebase database - REAL TIME
private void taskDeletion(DataSnapshot dataSnapshot) {
for (DataSnapshot singleDataSnapshot : dataSnapshot.getChildren()) {
String currentTask = singleDataSnapshot.getValue(String.class);
for (int i = 0; i < listOfTask.size(); i++) {
if (listOfTask.get(i).getTask().equals(currentTask)) {
listOfTask.remove(i);
}
}
//After removing , send notification to RecyclerView to remove the element
// AND re adapt it
mRecyclerAdapter.notifyDataSetChanged();
mRecyclerAdapter = new RecyclerViewAdapter(MainActivity.this, listOfTask);
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}
private void getAllTask(DataSnapshot dataSnapshot) {
//get all task when opening the app.
for(DataSnapshot singleShot : dataSnapshot.getChildren()){
String taskTitle = singleShot.getValue(String.class);
listOfTask.add(new Task(taskTitle));
mRecyclerAdapter = new RecyclerViewAdapter(MainActivity.this,listOfTask);
mRecyclerView.setAdapter(mRecyclerAdapter);
}
}}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<Task> tasks;
protected Context context;
public RecyclerViewAdapter(Context context, List<Task> allTask){
this.tasks = allTask;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerViewHolders viewHolders = null;
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, parent,false);
viewHolders = new RecyclerViewHolders(view,tasks);
return viewHolders;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.categoryTitle.setText(tasks.get(position).getTask());
}
@Override
public int getItemCount() {
return this.tasks.size();
}
}
RecyclerViewHolder
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
private static final String TAG = RecyclerViewHolders.class.getSimpleName();
public ImageView markIcon;
public TextView categoryTitle;
public ImageView deleteIcon;
private List<Task> taskObject;
public RecyclerViewHolders(View itemView, final List<Task> taskObject) {
super(itemView);
this.taskObject = taskObject;
categoryTitle = (TextView) itemView.findViewById(R.id.task_title);
markIcon = (ImageView) itemView.findViewById(R.id.task_icon);
deleteIcon = (ImageView) itemView.findViewById(R.id.task_delete);
deleteIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String taskTitle = taskObject.get(getAdapterPosition()).getTask();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query deletequery = reference.orderByChild("task").equalTo(taskTitle);
deletequery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
appleSnapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Cancelled Exception", databaseError.toException());
}
});
}
});
}}
Task.java
public class Task {
private String task;
public Task() {
}
public Task(String task) {
this.task = task;
}
public String getTask() {
return task;
}
}