【问题标题】:how do i update my listView after deleting the data in firebase realtime database删除firebase实时数据库中的数据后如何更新我的listView
【发布时间】:2018-08-22 18:10:04
【问题描述】:

我的应用程序将数据保存在 firebase 实时数据库中,我检索了 listView 中的数据。现在我想删除 listView 中的特定数据,我使用了 removeValue()。但问题是我无法删除后更新列表视图。它要么清除列表视图,要么在列表视图中只保留一个项目,当我重新打开我的应用程序时,这些项目已正确更新。请帮助。

这是我的 MainActivity.java

package com.example.manuj.todolist;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
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.Query;
import com.google.firebase.database.ValueEventListener;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    TextView textView;
    Button button;


    String tasks;
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference rootRef = db.getReference();
    DatabaseReference dataRef = rootRef.child("Todo");
    ArrayList<String>arrayList=new ArrayList<>();
    ArrayAdapter arrayAdapter;


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

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


        arrayAdapter=new ArrayAdapter<String>(this,R.layout.list_items,R.id.taskText,arrayList);
        listView.setAdapter(arrayAdapter);

        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);
                arrayAdapter.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) {

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                Toast.makeText(MainActivity.this,"sffad",Toast.LENGTH_SHORT).show();

            }

        });


        //Get the floating action button and set onClickLiter
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,CustomDialog.class);
                startActivity(intent);
            }
        });

    }


    public void deleteTask(View view) {

        int index=listView.getPositionForView(view);
        String str= (String) arrayAdapter.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());
            }
        });


        arrayAdapter.clear();
        arrayAdapter.addAll(arrayList);
        arrayAdapter.notifyDataSetChanged();

    }

}

【问题讨论】:

  • 为什么要清除整个适配器,在调用 appleSnapshot.getRef().removeValue(); 之后,只需从列表中删除该位置项,然后调用 arrayAdapter.notifyDataSetChanged();
  • 我该怎么做,我是初学者,请帮忙。
  • 在调用 appleSnapshot.getRef().removeValue() 的 for 循环之后,然后从数组列表中删除该位置项,例如 arraylist.remove(yourposition); b>,然后调用 notifydatasetchanged 并检查。
  • 非常感谢。我做了这个 changes.arrayList.remove(listView.getPositionForView(view));
  • 欢迎朋友,我已将上述建议评论添加为我的回答,你会接受吗?

标签: android firebase listview


【解决方案1】:

调用appleSnapshot.getRef().removeValue();的for循环后,从arraylist中删除该位置项,然后调用notifydatasetchanged并检查如下

public void deleteTask(View view) {

    int index=listView.getPositionForView(view);
    String str= (String) arrayAdapter.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();
            }
            arrayList.remove(listView.getPositionForView(view)); // remove the item from list
            arrayAdapter.notifyDataSetChanged();// notify the changed
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("TAG", "onCancelled", databaseError.toException());
        }
    });

}

【讨论】:

  • 如果您从 firebase 数据库中删除数据,则不会立即更新。
  • 我也是这样做的。感谢您的回答和时间
  • @ManujSharma,欢迎朋友 :)
猜你喜欢
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
相关资源
最近更新 更多