2017-07-17 58 views
0

我有一個代碼:Android的ListView的更新項目,火力地堡數據庫

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ......... 

    listViewMyAccountSettings = (ListView) findViewById(R.id.listViewMyAccountSettings); 
    arrayList = new ArrayList(); 
    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList); 
    listViewMyAccountSettings.setAdapter(arrayAdapter); 
    arrayAdapter.notifyDataSetChanged(); 



    listViewMyAccountSettings.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      if (position == 0){ 
       showAlertUsername(); 
      } else if (position == 1){ 
       showAlertAge(); 

      } 
      ...................... 
     } 
    }); 
     ................. 
    FirebaseDatabase database = FirebaseDatabase.getInstance(); 
    DatabaseReference myRef = database.getReference("userdata").child(myEmail).child("username"); 
    myRef.addValueEventListener(new ValueEventListener() { 
     @SuppressLint("SetTextI18n") 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // This method is called once with the initial value and again 
      // whenever data at this location is updated. 
      username = dataSnapshot.getValue(String.class); 
      arrayList.add("Your username: " + username); 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
      Log.w("TAG", "Failed to read value.", error.toException()); 
     } 
    }); 

有showAlertUsername方法:

private void showAlertUsername() { 
    alertDialogBuilder = new AlertDialog.Builder(
      MyAccountSettings.this); 
    input = new EditText(MyAccountSettings.this); 
    lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.MATCH_PARENT); 
    input.setLayoutParams(lp); 
    alertDialogBuilder.setView(input); 
    alertDialogBuilder.setPositiveButton("Discard", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
        Toast.makeText(getApplicationContext(), "No changes were made", 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 

    alertDialogBuilder 
      .setTitle("USERNAME") 
      .setMessage("Enter new username") 
      .setCancelable(false) 
      .setNegativeButton("Change", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          if (!input.getText().toString().isEmpty()) { 
           mDatabase.child("userdata").child(myEmail).child("username").setValue(input.getText().toString()); 
           Toast.makeText(getApplicationContext(), "Your username was changed successfully", 
             Toast.LENGTH_LONG).show(); 
           listViewMyAccountSettings.invalidateViews(); 
          } else { 
           Toast.makeText(getApplicationContext(), "Username can't be empty. No changes were made", 
             Toast.LENGTH_LONG).show(); 
          } 
         } 
        }); 

    AlertDialog alert = alertDialogBuilder.create(); 
    alert.show(); 
} 

我要更新列表視圖特定領域當我改變值(和將其設置爲數據庫)。問題是,當我更改數據庫中的值時,它會添加一個新項目以新值顯示列表視圖?有沒有辦法在不重新啓動活動的情況下更新舊的?謝謝。

回答

0

每次火力數據改變你添加一個新的項目列表:

public void onDataChange(DataSnapshot dataSnapshot) { 
    // This method is called once with the initial value and again 
    // whenever data at this location is updated. 
    username = dataSnapshot.getValue(String.class); 
    arrayList.add("Your username: " + username); 
} 

清除列表,如果在此之前,如果你想有一個條目。

public void onDataChange(DataSnapshot dataSnapshot) { 
    // This method is called once with the initial value and again 
    // whenever data at this location is updated. 
    arrayList.clear(); 
    username = dataSnapshot.getValue(String.class); 
    arrayList.add("Your username: " + username); 
    arrayAdapter.notifyDataSetChanged(); 
} 
+0

是的,我想到了他的方式,但我有3種onDataChange方法,因爲我已經在數據庫3個字段更新(用戶名,年齡和性別),因此這將刪除2個項目,只有1將在陣列 – Samo

+0

留也許listview不是你的用例的理想組件。或者,您可以聽取基礎'.child(myEmail)'表上的更改並獲取您感興趣的3個字段中的一個更改事件。 – Natan

0

你會改變數據如果您當前的ArrayList包含否則只需添加新的:

public void onDataChange(DataSnapshot dataSnapshot) { 
    username = dataSnapshot.getValue(String.class); 
    String item = "Your username: " + username; 

    if(arrayList.contains(item)) { 
     //if current arrayList contains the item just change it 
     arrayList.set(arrayList.indexOf(item), item); 
    } else { 
     //otherwise add the new one 
     arrayList.add(item); 
    } 
    //assuming your data is mutable 
    arrayAdapter.notifyDataSetChanged(); 
} 
0

使用ChildEventListener代替ValueEventListener.ChildEventListener給出了下面的方法上,

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

    @Override 
    public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
    // here in dataSnapshot you will the detail for which the value is changed,using this you can update the list. 
    } 

    @Override 
    public void onChildRemoved(DataSnapshot dataSnapshot) {} 

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

    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
0

@Priya感謝你使用了一段答案,代碼現在看起來就是這樣,它正在做它應該做的事情。

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
    DatabaseReference myRef = database.getReference("userdata").child(myEmail); 
    myRef.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) { 
      if (dataSnapshot.getKey().equals("username")) { 
       username = (String) dataSnapshot.getValue(); 
       arrayList.add("Username: " + username); 
      } else if (dataSnapshot.getKey().equals("age")) { 
       age = (String) dataSnapshot.getValue(); 
       arrayList.add("Age: " + age); 
      } else if (dataSnapshot.getKey().equals("gender")) { 
       gender = (String) dataSnapshot.getValue(); 
       arrayList.add("Gender: " + gender); 
      } 
     } 
     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) { 
      if (dataSnapshot.getKey().equals("username")) { 
       username = (String) dataSnapshot.getValue(); 
       arrayList.set(2, "Username: " + username); 
      } else if (dataSnapshot.getKey().equals("age")) { 
       age = (String) dataSnapshot.getValue(); 
       arrayList.set(0, "Age: " + age); 
      } else if (dataSnapshot.getKey().equals("gender")) { 
       gender = (String) dataSnapshot.getValue(); 
       arrayList.set(1, "Gender: " + gender); 
      } 
      arrayAdapter.notifyDataSetChanged(); 
     } 
     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) {} 
     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {} 
     @Override 
     public void onCancelled(DatabaseError databaseError) {} 
    });