2017-03-02 80 views
0

數據庫發現-KeAi52QSaiuf7p5jEYM沒有setter /場火力地堡數據庫沒有檢索數據W/ClassMapper:階級

"userinformation": [ 
    "-KeAi52QSaiuf7p5jEYM" : { 
     "website" : "test1" 
     "username" : "test1" 
    } 
} 

類我的資料不檢索數據

package **.****; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.TextView; 
import android.view.Menu; 
import android.view.MenuItem; 

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.ValueEventListener; 

import ****.****.m_Model.useri1; 



public class myprofile extends AppCompatActivity { 


    private TextView myusername111; 
    private DatabaseReference mFirebaseDatabase; 
    private FirebaseDatabase mFirebaseInstance; 


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




     myusername111 = (TextView) findViewById(R.id.textview22); 
     mFirebaseInstance = FirebaseDatabase.getInstance(); 

     mFirebaseInstance.getReference("userinformation").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       useri1 model = dataSnapshot.getValue(useri1.class); 

      myusername111.setText(model.getusername()); 


      } 
      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 


     }); 




    } 

類edit_profile

package package ***.****; 

import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 

import ***.***.m_Model.useri1; 




public class edit_profile extends AppCompatActivity { 
    private Toolbar toolbar; 






    private EditText website; 
    private EditText username; 
    private Button bsubmit; 
    private DatabaseReference mFirebaseDatabase; 
    private FirebaseDatabase mFirebaseInstance; 


    private android.view.ViewGroup parent; 


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


     setContentView(R.layout.activity_edit_profile); 


     mFirebaseInstance = FirebaseDatabase.getInstance(); 
     mFirebaseDatabase = mFirebaseInstance.getReference("users"); 
     usernamr = (EditText) findViewById(R.id.usernamr); 
     website = (EditText) findViewById(R.id.website); 
     Button bsubmit = (Button) findViewById(R.id.b_submit); 


       bsubmit.setOnClickListener(new View.OnClickListener() { 


        @Override 
        public void onClick(View v) { 

         if (!isEmpty(website, username) && !isEmpty(website, username)) { 
          mNewprofile("53", website.getText().toString().trim(), username.getText().toString().trim()); 
          finish(); 
          startActivity(new Intent(getApplicationContext(), myprofile.class)); 
         } 
        } 


     }); 



    } 
    private void mNewprofile(String s, String username, String website) { 



     new_profile1 userinformation1 = new new_profile1(website, username); 

     mFirebaseDatabase.child("userinformation").setValue(userinformation1); 
    } 

class useri1

package **.****.m_Model; 



public class useri1 { 


    public String username; 
    public String website; 


    public Update_user1() { 
    } 
    public Update_user1(String username,String website) { 
     this.website = website; 
     this.username = username; 


    } 

    public String getwebsite() { 

     return website; 
    } 

    public String getusername() { 

     return username; 
    } 
    public void setwebsite(String website) { 
      this.website = website; 

     } 
    public void setusermame(String usermame) { 
     this.usermame = usermame; 

    } 

錯誤:

W/ClassMapper: No setter/field for -KeAi52QSaiuf7p5jEYM found on class ***.****.m_Model.useri1

回答

0

你檢索的userinformation值。所以,你在onDataChange得到快照都會有這樣的價值:

"-KeAi52QSaiuf7p5jEYM" : { 
    "website" : "test1" 
    "username" : "test1" 
} 

一個useri1websiteusername財產。但在上面的JSON中,只有一個名爲-KeAi52QSaiuf7p5jEYM的屬性。所以這兩個不匹配,導致你得到的錯誤。

爲了解決這個問題,你應該做幾件事情:

  • 環比孩子們在快照
  • 徹底簡化您的POJO

產生的代碼將是:

mFirebaseInstance.getReference("userinformation").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) { 
      MyUser model = dataSnapshot.getValue(MyUser.class); 
      myusername111.setText(model.username); 
     } 
    } 
    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     throw databaseError.toException(); // don't ignore errors 
    } 
}); 

public class MyUser { 
    public String username; 
    public String website; 
} 

可能也使用cl屁股與吸氣劑和二傳手。但在這種情況下,請確保getter遵循Java Bean的大小寫屬性規則。所以:

public class MyUser { 
    private String username; 
    private String website; 
    public MyUser() { 
    } 
    public String getUsername() { return username; } 
    public void setUsername(string value) { username = value; } 
    public String getWebsite() { return website; } 
    public void setWebsite(string value) { website = value; } 
} 
0

您應該使用ChildEventListener而不是ValueEventListener

因爲您正在使用ValueEventListener Firebase正在嘗試設置名爲-KeAi52QSaiuf7p5jEYM的屬性,但它不是屬性,而是屬性。

添加ChildEventListener代碼如下:

mFirebaseInstance.getReference("userinformation").addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) { 
      Update_user1 user = dataSnapshot.getValue(Update_user1.class); 
      // ... 
     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String previousKey) { 
      // ... 
     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String previousKey) { 
      // ... 
     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 
      // ... 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      // ... 
     } 
    });