How to retrieve POJO in Firebase Android - android

public class MainActivity extends AppCompatActivity {
private Firebase mRef;
private EditText mName,mEmail,mAge;
private Button mAddStud;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mName = (EditText) findViewById(R.id.editTextName);
mEmail = (EditText) findViewById(R.id.editTextEmail);
mAge = (EditText) findViewById(R.id.editTextAge);
mAddStud = (Button) findViewById(R.id.addStudents);
mRef = new Firebase(Constants.FIREBASE_URL);
mAddStud.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Student student = new Student(mName.getText().toString(),mEmail.getText().toString(), Integer.parseInt(mAge.getText().toString()));
mRef.child("newStudent").setValue(student);
}
});
}
}
Display
public class Display extends AppCompatActivity {
private Firebase mRef;
private TextView mDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mDisplay = (TextView) findViewById(R.id.textViewDisplay);
mRef = new Firebase("https://firecrestapp.firebaseio.com/newStudent");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Student value = dataSnapshot.getValue(Student.class);
mDisplay.setText(value.getName()+" "+value.getEmailId()+" "+value.getAge() );
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
Im getting this error
com.firebase.client.FirebaseException: Failed to bounce to type
How am I supposed to retrieve the data?

Related

Unable to increment Firebase database from Android

I'm trying to add a data to Firebase and Increment its Child.
I'm getting it done until the value/Id is three. Then when I inserted a data for the fourth time, it replaces one of the data and can't increment more than three. So, what could be the problem?
This is my Java Code
public class EnterInkhawmProg extends AppCompatActivity {
EditText tv1, tv2, tv3, tv4, tv5, tv6, tv7;
Button btn;
Program program;
long maxid = 0;
DatabaseReference ref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inkhawp_prog);
tv1 = findViewById(R.id.edittext_date);
tv2 = findViewById(R.id.edittext_chairman);
tv3 = findViewById(R.id.edittext_tantu);
tv4 = findViewById(R.id.edittext_thilpek);
tv5 = findViewById(R.id.edittext_biblechhiar);
tv6 = findViewById(R.id.edittext_thusawitu);
tv7 = findViewById(R.id.edittext_hunhmandan);
program = new Program();
ref = FirebaseDatabase.getInstance().getReference().child("Inkhawm");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
maxid = (dataSnapshot.getChildrenCount());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
btn = findViewById(R.id.sub_btn);
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
program.setDate(tv1.getText().toString().trim());
program.setChairman(tv2.getText().toString().trim());
program.setTantu(tv3.getText().toString().trim());
program.setThilpek(tv4.getText().toString().trim());
program.setBiblechhiarrual(tv5.getText().toString().trim());
program.setThusawitu(tv6.getText().toString().trim());
program.setHunhmandan(tv7.getText().toString().trim());
ref.child(String.valueOf(maxid+1)).setValue(program);
Toast.makeText(EnterInkhawmProg.this, "Success", Toast.LENGTH_SHORT).show();
tv1.setText("");
tv2.setText("");
tv3.setText("");
tv4.setText("");
tv5.setText("");
tv6.setText("");
tv7.setText("");
}
}
just replace this line
ref.child(String.valueOf(maxid+1)).setValue(program);
with
ref.child(String.valueOf(maxid+1)).push().setValue(program);

android with firebase RealTime Database

how to fix this issue "savedInstance null" when using firebase realtimeDataBase
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance( );
DatabaseReference myRef = database.getReference();
myRef.child("test").push().setValue("Nikmal");
Toast.makeText(getApplicationContext(),"succesfully", Toast.LENGTH_SHORT).show();
}
});
}
}
Try this code, it should works if you use a DatabaseReference
Button btn;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRef = FirebaseDatabase.getInstance().getReference();
btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myRef.child("test").push().setValue("Nikmal");
Toast.makeText(getApplicationContext(),"succesfully", Toast.LENGTH_SHORT).show();
}
});
}

Can't update the value of textview by settext

My settext do not update after I have updated it's value from database. But if I logout and login into my app settext is refreshed.I get the value of settext by SharedPreferences.
public class MySettingsActivity extends AppCompatActivity {
private TextView settingsDisplayName;
private TextView settingsDisplayStatus;
private User user = SharedPrefManager.getInstance(this).getUser();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_settings);
settingsDisplayName = (TextView) findViewById (R.id.settings_username);
settingsDisplayStatus = (TextView) findViewById(R.id.settings_status);
settingsChangeUsername.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String old_name = settingsDisplayName.getText().toString();
Intent usernameIntent = new Intent(MySettingsActivity.this, UsernameActivity.class);
usernameIntent.putExtra("user_name",old_name );
startActivity(usernameIntent);
}
});
}
#Override
protected void onStart() {
MySettingsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
settingsDisplayName.setText(user.getName());
settingsDisplayStatus.setText(user.getEmail());
}
});
super.onStart();
}
}

Failed to convert value of type java.util.HashMap to String and app crashes

public class MainActivity extends AppCompatActivity {
//points to the root node of the database
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mConditionRef = mRootRef.child("condition");
TextView content;
Button sunny, foggy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (TextView) findViewById(R.id.condition);
sunny = (Button) findViewById(R.id.sunny);
foggy = (Button) findViewById(R.id.foggy);
}
#Override
protected void onStart() {
super.onStart();
mConditionRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
content.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
sunny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Sunny");
mRootRef.child("condition").push().setValue("Abhijeet Jain2");
}
});
foggy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Foggy");
}
});
}
}
The string gets added in the realtime database with a automatic made id but the app crashes as soon as I press the button "sunny".
What could be the reason and the solution?

Search for products in Firebase

public class SearchActivity extends AppCompatActivity {
private EditText InputString;
private Button btnScan, btnSearch;
private DatabaseReference mFirebaseDatabaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
InputString = (EditText) findViewById(R.id.input_search);
btnScan = (Button) findViewById(R.id.btn_Scan);
btnSearch = (Button) findViewById(R.id.btn_Search);
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String checkString = "InputString";
Query query = mFirebaseDatabaseReference.child("Stock").orderByChild("productName").orderByChild("productId").equalTo(checkString);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
setContentView(R.layout.activity_product__detail_);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
I want to do let the users search if the users key in the "Product name", "Product code" or using "barcode" to search the product and then the application will display the product detail.For example, the users enter "mild steel", the application will display the product name, product code, barcode and the quantity
Here sample code
final String checkString = "inputString";
Query query = mDatabase.child("user").orderByChild("productName").equalTo(checkString);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
mDatabase.child("e-inventory-store").chile("Stock")
.addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
String productName = map.get("productName");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this one,

Categories

Resources