0

Firstly I would like to apologize for my poor English.

I just started my adventure with android studio and followed some tutorials and I have a question about one of them. I was looking in many places for this answer but I found none that would satisfy my needs.

I created a chat app in android studio with FIREBASE as backend database but I would like to add some extra functionality to it. Namely remove item.

I’ll post my code here, can someone tell me how can I make it possible to delete Item that I pressed on from the FirebaseListAdapter.

MainActivity:

public class MainActivity extends ListActivity {
private Firebase mFirebaseRef;
FirebaseListAdapter<ChatMessage> mListAdapter;

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


    Firebase.getDefaultConfig().setPersistenceEnabled(true);
    Firebase.setAndroidContext(this);

    mFirebaseRef = new Firebase("https://shining-heat-1471.firebaseio.com");


    final EditText textEdit = (EditText) this.findViewById(R.id.text_edit);
    Button sendButton = (Button) this.findViewById(R.id.send_button);

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = textEdit.getText().toString();
            ChatMessage message = new ChatMessage("Android User", text);
            mFirebaseRef.push().setValue(message);
            textEdit.setText("");
        }
    });


    mListAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
            android.R.layout.two_line_list_item, mFirebaseRef) {
        @Override
        protected void populateView(View v, ChatMessage model) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(model.getName());
            ((TextView)v.findViewById(android.R.id.text2)).setText(model.getText());
        }
    };
    setListAdapter(mListAdapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mListAdapter.cleanup();
}

ChatMessage:

public class ChatMessage {
private String name;
private String text;

public ChatMessage() {
    // necessary for Firebase's deserializer
}
public ChatMessage(String name, String text) {
    this.name = name;
    this.text = text;
}

public String getName() { return name; }

public String getText() { return text; }
}
Apo
  • 3
  • 1
  • 3

1 Answers1

7

I'll leave it to you to figure out what item the user clicked on (but this might be a good start).

Once you know the position of the item the user clicked on, you can remove it from Firebase easily with:

Firebase itemRef = adapter.getRef(position);
itemRef.removeValue();

This will remove the item from the database and from the list adapter.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • "itemRef.remove();" when I try this it doesn't work, but when I use this methode: "itemRef.removeValue();" it works fine. – Apo Dec 01 '15 at 20:50
  • Thanks for catching that. It's called remove() in the JavaScript API, but obviously not in the Android one. I've updated the code in my answer. – Frank van Puffelen Dec 01 '15 at 22:02