Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android应用程序中从Firebase实时数据库中删除数据(json的特定节点)_Android_Firebase - Fatal编程技术网

如何在android应用程序中从Firebase实时数据库中删除数据(json的特定节点)

如何在android应用程序中从Firebase实时数据库中删除数据(json的特定节点),android,firebase,Android,Firebase,我用onClick listener(名为delteTask)构建了一个Todo list应用程序。我想在单击delete按钮时删除firebase中的数据。我在文档中签入了数据库引用,要删除节点,可以使用removeValue()方法。但我不知道如何使用它。我也编写了代码,请帮我做这件事 **MY MAINACTIVTY>JAVA** package com.example.manuj.todolist; import android.content.DialogInterface;

我用onClick listener(名为delteTask)构建了一个Todo list应用程序。我想在单击delete按钮时删除firebase中的数据。我在文档中签入了数据库引用,要删除节点,可以使用removeValue()方法。但我不知道如何使用它。我也编写了代码,请帮我做这件事

**MY MAINACTIVTY>JAVA**
package com.example.manuj.todolist;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    String tasks;

    FirebaseDatabase db=FirebaseDatabase.getInstance();
    DatabaseReference rootRef=db.getReference();
    DatabaseReference dataRef=rootRef.child("Todo");


    TextView textView;
    Button button;
    ListView listView;
    ArrayAdapter mAdapter;

    ArrayList<String> arrayList=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=findViewById(R.id.button);
        textView=findViewById(R.id.textView);
        listView=findViewById(R.id.listView);


        mAdapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView,arrayList);
        listView.setAdapter(mAdapter);


        dataRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value=dataSnapshot.getValue().toString();
                try {
                    JSONObject object=new JSONObject(value);
                    tasks=object.getString("Task");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                arrayList.add(tasks);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.addTask:
                final EditText editText=new EditText(this);
                AlertDialog dialog=new AlertDialog.Builder(this)
                        .setTitle("Add new Task")
                        .setMessage("What's your Task?")
                        .setView(editText)
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String task= editText.getText().toString();
                                HashMap<String,String> todoMap=new HashMap<String, String>();
                                todoMap.put("Task",task);
                                dataRef.push().setValue(todoMap);

                            }
                        })
                        .setNegativeButton("CANCEL",null).create();
                dialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);

    }
    public void deleteTask(View view) {

        DatabaseReference removeRef=FirebaseDatabase.getInstance().getReference("Todo");
        removeRef.removeValue();//the problem i n using the above is it removes my all data,but how
        //do i  remove a particular.

    }
}
**我的主要活动>JAVA**
包com.example.manuj.todolist;
导入android.content.DialogInterface;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.firebase.database.ChildEventListener;
导入com.google.firebase.database.DataSnapshot;
导入com.google.firebase.database.DatabaseError;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.database.ValueEventListener;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类MainActivity扩展了AppCompatActivity{
字符串任务;
FirebaseDatabase db=FirebaseDatabase.getInstance();
DatabaseReference rootRef=db.getReference();
DatabaseReference dataRef=rootRef.child(“Todo”);
文本视图文本视图;
按钮;
列表视图列表视图;
ArrayAdapter mAdapter;
ArrayList ArrayList=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=findViewById(R.id.button);
textView=findViewById(R.id.textView);
listView=findViewById(R.id.listView);
mAdapter=newArrayAdapter(这个,R.layout.list_项,R.id.textView,arrayList);
setAdapter(mAdapter);
dataRef.addChildEventListener(新的ChildEventListener(){
@凌驾
公共void onChildAdded(DataSnapshot DataSnapshot,字符串s){
字符串值=dataSnapshot.getValue().toString();
试一试{
JSONObject对象=新的JSONObject(值);
tasks=object.getString(“任务”);
}捕获(JSONException e){
e、 printStackTrace();
}
添加(任务);
mAdapter.notifyDataSetChanged();
}
@凌驾
公共void onChildChanged(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
ChildRemoved上的公共void(DataSnapshot DataSnapshot){
}
@凌驾
已移动ChildMoved上的公共void(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单,菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.addTask:
最终编辑文本编辑文本=新编辑文本(本);
AlertDialog=新建AlertDialog.Builder(此)
.setTitle(“添加新任务”)
.setMessage(“您的任务是什么?”)
.setView(编辑文本)
.setPositiveButton(“添加”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
String task=editText.getText().toString();
HashMap todoMap=新HashMap();
todoMap.put(“任务”,任务);
dataRef.push().setValue(todoMap);
}
})
.setNegativeButton(“取消”,null).create();
dialog.show();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
公共void deleteTask(视图){
DatabaseReference removeRef=FirebaseDatabase.getInstance().getReference(“Todo”);
RemoveEF.removeValue();//使用上述方法的问题是它会删除我的所有数据,但如何删除
//我是否删除某个特定的。
}
}

您需要尝试访问的记录的密钥。看看Json是如何构造的会很有帮助

DatabaseReference removeRef=FirebaseDatabase.getInstance().getReference("Todo").child("RECORD KEY").removeValue();

我在回答我的问题,呵呵,但是我花了时间来确定问题。所以,我不会删除这个问题,让人们知道这类问题的答案,这样他们就不会面临和我一样的问题

好的,这是答案

public void deleteTask(View view) {
        int index=listView.getPositionForView(view);
        String str= (String) mAdapter.getItem(index++);

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
        Query taskQuery = ref.child("Todo").orderByChild("Task").equalTo(str);
        taskQuery.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", "onCancelled", databaseError.toException());
            }
        });
        mAdapter.notifyDataSetChanged();
    }

哪个键………?你能在Manuj Sharma共享你的数据库结构吗