Firebase android studio从FirebaseListAdapter中移除项目 [英] Firebase android studio remove item from FirebaseListAdapter

查看:228
本文介绍了Firebase android studio从FirebaseListAdapter中移除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想为我可怜的英语道歉。

我刚开始使用android studio进行冒险,接下来是一些教程,我对其中的一个问题有疑问。我在许多地方寻找这个答案,但我发现没有满足我的需要。

我创建了一个聊天应用程序与Android作为后端数据库的FIREBASE,但我会喜欢添加一些额外的功能。即将删除项目。

我会在这里发布我的代码,有人可以告诉我如何删除从FirebaseListAdapter中按下的项目。
$ b $ MainActivity:

pre $ 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 =新的Firebase(https://shining-heat-1471.firebaseio.com);


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

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

$ b 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();

$ / code>

ChatMessage:

  public class ChatMessage {
private String name;
私人字符串文本;
$ b公共ChatMessage(){
// Firebase的反序列化程序所必需的
}
public ChatMessage(String name,String text){
this.name =名称;
this.text = text;
}

public String getName(){return name; }

public String getText(){return text; }
}


解决方案

找出用户点击的项目(但可能是一个好开始)。

一旦您知道用户点击的项目的位置,您可以轻松地将其从Firebase中移除:

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

这将从列表适配器的数据库中移除项目。

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; }
}

解决方案

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.

这篇关于Firebase android studio从FirebaseListAdapter中移除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆