0
我正在開發聊天應用程序,而我剛剛開始。我有一個活動,用戶可以打開聊天室並刪除。我的問題是與刪除。我可以成功從列表中刪除聊天室,但我不知道如何從Firebase中將其刪除。 問題是我使用longclick,我的意思是如果用戶想要刪除聊天室,他需要點擊聊天室,但長按一下,然後刪除選項彈出。然後按下「刪除」按鈕並從列表中刪除它。Android - 如何刪除Firebase中的房間
聊天室代碼
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)
方法獲取列表的ID,並從列表中刪除。 但我不知道如何從firebase中刪除它。在firebase中,我將其保存爲根。如果代碼丟失,請讓我知道這樣我就可以上傳更多的代碼
編輯 - 創建聊天代碼
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("");
}
});
我已經添加了創建聊天室的代碼 –
確定我認爲我的代碼應該可以工作 - 你試過嗎? – Benoit
是的,它的工作!你做對了! –