2017-05-05 6 views
0

//college_landingpage.java実際に更新ボタンがクリックされたとき、私はアドレスと説明を更新したい。 update()は動作していません。

private DatabaseReference mref; 
    private ImageView college_profile_pic; 
    ArrayList<College> colleges; 
    private TextView address; 
    private TextView description; 


    private Button btnupdate; 

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

     mref = FirebaseDatabase.getInstance().getReference(); 
     address = (TextView) findViewById(R.id.address); 
     college_profile_pic = (ImageView) findViewById(R.id.college_profile_pic); 
     description = (TextView) findViewById(R.id.description); 
     btnupdate = (Button) findViewById(R.id.btnupdate); 
     colleges=new ArrayList<>(); 

     mref.child("Colleges").addChildEventListener(new ChildEventListener() { 
                 @Override 
                 public void onChildAdded(DataSnapshot snapshot, String s) { 
                  if (snapshot.hasChildren()) { 
                   for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
                    College college = postSnapshot.getValue(College.class); 
                    String add = "Address :" + college.getAddress() + "\n"; 
                    String descr = "\n Description" + college.getDescription() + "\n\n"; 

                    address.setText(add); 
                    description.setText(descr); 

                   } 
                  } 
                 } 

                 @Override 
                 public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
                  String key=dataSnapshot.getKey(); 
                  College newcollege=dataSnapshot.getValue(College.class); 
                  for(College cl:colleges){ 
                   if (cl.getKey().equals(key)){ 
                    cl.setValues(newcollege); 
                    break; 
                   } 
                  } 

                 } 

                 @Override 
                 public void onChildRemoved(DataSnapshot dataSnapshot) { 

                 } 

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

                 } 

                 @Override 
                 public void onCancelled(DatabaseError databaseError) { 
                  System.out.println("The read failed: " + databaseError.getMessage()); 
                 } 
     } 



     ); 



    } 
    public void update(College college, String newaddress, String newdescription) { 
     college.setAddress(newaddress); 
     college.setDescription(newdescription); 
     mref.child("colleges").child(college.getKey()).setValue(college); 
    } 


    @Override 
    public void onClick(View v) { 
     if (v==btnupdate) 

      update(colleges,address,description); 
    } 
} 

//College.java 
public class College { 
    String name,address,description; 
    private String key; 

    public College() { 

    } 

    public String getKey(){ 
     return key; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public void setValues(College newcollege) { 
     address=newcollege.address; 
     description=newcollege.description; 
    } 
} 

// activity_college_landingpage

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_college_landingpage" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.learn.binod.navigationdrawer.College_landingpage"> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" 
     android:layout_centerHorizontal="true"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/ic_institution" 
     android:layout_alignParentTop="true" 
     android:layout_margin="15dp" 
     android:layout_marginTop="11dp" 
     android:id="@+id/college_profile_pic" /> 

    <TextView 
     android:text="Descrition" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/description" 
     android:maxLines="3" 
     android:layout_margin="15dp" 
     android:layout_marginBottom="95dp" 
     android:layout_above="@+id/textView4" /> 

    <TextView 
     android:text="address" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="154dp" 
     android:layout_margin="15dp" 
     android:id="@+id/address" 
     android:layout_alignParentBottom="true" 
     android:layout_alignEnd="@+id/description" 
     android:maxLines="3" 
     android:layout_marginEnd="12dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnupdate" 
     android:text="@string/button_update" /> 
    </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

私はbtnupdateがclicked.the更新ボタンはアドレスと説明 方法を更新することができるはずであるとき、更新を実行する方法を知りたいですそのイベントを行うには?

my database structure

答えて

0

子どもたちを更新する最も簡単な方法は、refrenceに直接setValue()方法を使用することです。 newAddressnewDescriptionnewNameを更新する新しい値がされている

mref.child("colleges").child(college.getKey()).child("address").setValue(newAddress); 
mref.child("colleges").child(college.getKey()).child("description").setValue(newDescription); 
mref.child("colleges").child(college.getKey()).child("name").setValue(newName); 

mref.child("colleges").child(college.getKey()).setValue(college); 

:だから、これを達成するために、この行を変更してください。

希望します。

+0

私は最後にupdate()メソッドのパラメータを知っておく必要があります。 @オーバーライド パブリックボイドonClick(ビューv){ if(v == btnupdate) 更新(colleges、address、description); } –

+0

'update()'メソッドに入れなければならないパラメータは、あなたの場所を更新するために必要な正確なパラメータです。 'newAddress'、' newDescription'、 'newName'です。 –

+0

public void update(カレッジカレッジ、文字列newaddress、文字列newdescription){ college.setAddress(newaddress); college.setDescription(newdescription);mref.child( "college")。子(college.getKey())。子( "address")。setValue(newaddress); mref.child( "college")。子(college.getKey())。子( "description")setValue(newdescription);子クラス( "new"); mref.child( "colleges")。子(college.getKey())。子( "名前")。 } @Override パブリックボイドonClick(ビューv){ if(v == btnupdate) 更新(colleges、address、description); } –

関連する問題