Android - 如何删除Firebase

时间:2016-11-08 20:31:31

标签: android listview firebase firebase-realtime-database

我正在开发一个聊天应用程序,我刚刚开始。我有一项活动,用户可以打开聊天室并删除。我的问题是删除。我可以从列表中成功删除聊天室,但我不知道如何从Firebase中删除它。 问题是我正在使用longclick,我的意思是如果用户想要移除一个聊天室,他需要点击聊天室但是长按一下,然后弹出删除选项。然后他按下“删除”按钮并从列表中删除它。

聊天室代码

   public class MainActivity extends AppCompatActivity {

private Button createChat;
private EditText chatRoomName;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> listOfChats = new ArrayList<>();
private String name;
private DatabaseReference root  = FirebaseDatabase.getInstance().getReference().getRoot();

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

       ifUserIsLoggedIn(); // Method that checks if the user is logged in. if not, it will take the
                           // user to Login Activity
    createChat = (Button)findViewById(R.id.createChatBtn);
    chatRoomName = (EditText)findViewById(R.id.chatRoomEt);
    listView = (ListView)findViewById(R.id.listView);

    arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, listOfChats);

    listView.setAdapter(arrayAdapter);

        // this method delete a chat room after a long click when "delete" button press
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    switch(item.getItemId()){

        case R.id.delete:
            arrayAdapter.remove(arrayAdapter.getItem(info.position));
            root.removeValue(arrayAdapter.getItem(info.position));

    }

    return super.onContextItemSelected(item);
}
public boolean onContextItemSelected(MenuItem item)上的

    method获取列表的id并从列表中删除它。 但我不知道如何从firebase中删除它。在firebase中我将它保存为root。如果缺少代码,请告诉我,以便我可以上传更多代码

已编辑 - 创建聊天代码

    createChat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromInputMethod(chatRoomName.getWindowToken(),0);

            // send to the fire base the data. it sends only the name of the room, with no any key.
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(chatRoomName.getText().toString(), "");
            root.updateChildren(map);
            chatRoomName.setText("");
        }
    });

1 个答案:

答案 0 :(得分:0)

您的代码不能使用firebase 9.8进行编译。然后它无法工作,因为您在删除它之后引用了适配器的项目。最后,我认为在Firebase确认删除之前,您不应该从适配器中删除聊天室。

很难给出100%正确答案,因为您没有显示创建聊天室的代码。无论如何,我在这里假设聊天室是根目录下的孩子。试试这个:

@Override
public boolean onContextItemSelected(MenuItem item){
    final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {

        case R.id.delete:

            final String chatRoomToDelete = arrayAdapter.getItem(info.position)
            root.child(chatRoomToDelete).removeValue(new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                    if (databaseError == null) {
                        arrayAdapter.remove(chatRoomToDelete);
                    } else {
                        // show databaseError to user
                    }
                }
            });

    }

    return super.onContextItemSelected(item);
}