Home:ALL Converter>Android studio remove items from both listview and firebase

Android studio remove items from both listview and firebase

Ask Time:2018-01-14T11:42:28         Author:Pearl Fall

Json Formatter

I'm trying to remove items from both listview and firebase in android studio. When I remove items from the firebase console, it automatically removes items from listview. However I want to remove items from the listview first, and want this changes to be reflected on the database.

public class PreferActivity extends AppCompatActivity {
    //Firebase Var
    private DatabaseReference mDatabase;

//Android Layout
private Button btnAdd;
private EditText etText;
private ListView listView;

//Array list
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prefer);
    mDatabase= FirebaseDatabase.getInstance().getReference();

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    etText = (EditText) findViewById(R.id.etDatabase);
    listView = (ListView) findViewById(R.id.database_list_view);

    listView.setAdapter(adapter);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDatabase.push().setValue(etText.getText().toString());
        }
    });


    mDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String string = dataSnapshot.getValue(String.class);

            arrayList.add(string);

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            String string = dataSnapshot.getValue(String.class);

            arrayList.remove(string);

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



}
}

Any comments would help me. Thanks in advance

Author:Pearl Fall,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48246504/android-studio-remove-items-from-both-listview-and-firebase
Alex Mamo :

To solve this, use the following lines of code:\n\nlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {\n @Override\n public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {\n String item = adapter.getItem(position);\n adapter.remove(item);\n adapter.notifyDataSetChanged();\n //Delete item form Firebase database\n }\n});\n\n\nI have added a setOnItemClickListener on your listView and remove the item from the adapter first and them from your database accordingly to the item that was clicked.",
2018-01-14T09:31:24
yy