2015-10-27 91 views
1

因此,我正在構建一個使用android SDK和firebase作爲後端的簡單待辦應用程序,並且一切似乎都起作用,因爲數據將添加到我的Firebase存儲中,移動設備以及數據也會被刪除,但數據只會從Firebase存儲中刪除,並不會從移動設備/屏幕上刪除,因此我如何從活動中刪除數據。Android ListView當數據在firebase被刪除時刪除

這裏是MainActivity.java

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 

import com.firebase.client.ChildEventListener; 
import com.firebase.client.DataSnapshot; 
import com.firebase.client.Firebase; 
import com.firebase.client.FirebaseError; 
import com.firebase.client.ValueEventListener; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // Get ListView object from xml 
     final ListView listView = (ListView)findViewById(R.id.listView); 

     // Create a new Adapter 
     final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
       android.R.layout.simple_list_item_1, android.R.id.text1); 

     // Assign adapter to ListView 
     listView.setAdapter(adapter); 

     // Use Firebase to populate the list. 
     Firebase.setAndroidContext(this); 

     new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
       .addChildEventListener(new ChildEventListener() { 
        @Override 
        public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
         adapter.add((String)dataSnapshot.child("text").getValue()); 
        } 

        @Override 
        public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
         adapter.remove((String)dataSnapshot.child("text").getValue()); 
        } 

        @Override 
        public void onChildRemoved(DataSnapshot dataSnapshot) { 

        } 

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

        } 

        @Override 
        public void onCancelled(FirebaseError firebaseError) { 

        } 
       }); 

     // Add items via the Button and EditText at the bottom of the window. 
     final EditText text = (EditText)findViewById(R.id.todoText); 
     final Button btn = (Button)findViewById(R.id.addButton); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
         .push() 
         .child("text") 
         .setValue(text.getText().toString()); 
      } 
     }); 

     // Delete items when clicked 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
       AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this); 
       adb.setTitle("Delete?"); 
       adb.setMessage("Are you sure you want to delete " + position); 
       adb.setNegativeButton("Cancel", null); 
       adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         adapter.notifyDataSetChanged(); 
        } 
       }); 
       adb.show(); 
       new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
         .orderByChild("text") 
         .equalTo((String) listView.getItemAtPosition(position)) 
         .addListenerForSingleValueEvent(new ValueEventListener() { 
          @Override 
          public void onDataChange(DataSnapshot dataSnapshot) { 
           if(dataSnapshot.hasChildren()){ 
            DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next(); 
            firstChild.getRef().removeValue(); 
           } 
          } 

          @Override 
          public void onCancelled(FirebaseError firebaseError) {} 
         }); 
      } 
     }); 

    } 
} 

的MY-火力-APP ==已經改變了我的火力點的應用程序的名字,我只是把一個佔位符那裏。

需要幫助:)

+0

'notifyDataSetChanged()'? –

回答

2

編輯您的MainAcitvity.java像下面我希望它的作品。 有關詳細信息,請參閱下面的鏈接。 https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 

import com.firebase.client.ChildEventListener; 
import com.firebase.client.DataSnapshot; 
import com.firebase.client.Firebase; 
import com.firebase.client.FirebaseError; 
import com.firebase.client.ValueEventListener; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // Get ListView object from xml 
     final ListView listView = (ListView)findViewById(R.id.listView); 

     // Create a new Adapter 
     final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1); 

     // Assign adapter to ListView 
     listView.setAdapter(adapter); 

     // Use Firebase to populate the list. 
     Firebase.setAndroidContext(this); 

     new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
      .addChildEventListener(new ChildEventListener() { 
       @Override 
       public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
         adapter.add((String)dataSnapshot.child("text").getValue()); 
       } 

       @Override 
       public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
         adapter.remove((String)dataSnapshot.child("text").getValue()); 
       } 

       @Override 
       public void onChildRemoved(DataSnapshot dataSnapshot) { 
adapter.remove((String)dataSnapshot.child("text").getValue()); 
       } 

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

       } 

       @Override 
       public void onCancelled(FirebaseError firebaseError) { 

       } 
      }); 

    // Add items via the Button and EditText at the bottom of the window. 
    final EditText text = (EditText)findViewById(R.id.todoText); 
    final Button btn = (Button)findViewById(R.id.addButton); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
        .push() 
        .child("text") 
        .setValue(text.getText().toString()); 
     } 
    }); 

    // Delete items when clicked 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
      AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this); 
      adb.setTitle("Delete?"); 
      adb.setMessage("Are you sure you want to delete " + position); 
      adb.setNegativeButton("Cancel", null); 
      adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        new Firebase("https://MY-FIREBASE-APP.firebaseio.com/todoItems") 
        .orderByChild("text") 
        .equalTo((String) listView.getItemAtPosition(position)) 
        .addListenerForSingleValueEvent(new ValueEventListener() { 
         @Override 
         public void onDataChange(DataSnapshot dataSnapshot) { 
          if(dataSnapshot.hasChildren()){ 
           DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next(); 
           firstChild.getRef().removeValue(); 
          } 
         } 

         @Override 
         public void onCancelled(FirebaseError firebaseError) {} 
        }); 
        adapter.notifyDataSetChanged(); 
       } 
      }); 
      adb.show(); 

     } 
    }); 

    } 
} 
+0

感謝兄弟。它的工作 – Fadil

+0

不客氣:) – waleedsarwar86