Here is my code:
mFirebaseDatabae = FirebaseDatabase.getInstance();
mMessageReference = FirebaseDatabase.getInstance().getReference().child("memos");
serchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mMessageReference.orderByChild("title").equalTo("z").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(SerchActivity.this, "print", Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
Here is my Firebase tree:
Isn't it possible for the title to be printed only when the title is z?
Fatch like this way...
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String title= (String) messageSnapshot.child("title").getValue();
}
Related
I'm trying to implement a search function on my recyclerviewer but I'm having some trouble with it. I have created the query and the method for it,but the search does not return anything at all.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
...
mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUploads.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Buildings upload = postSnapshot.getValue(Buildings.class);
upload.setKey(postSnapshot.getKey());
mUploads.add(upload);
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
mButtonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String searchText = mSearchView.getText().toString();
firebaseUserSearch(searchText);
}
});
and i have this outside of my onCreate
private void firebaseUserSearch(String searchText) {
Query queryRef = mDatabaseRef.child("Buildings").orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot postsSnapshot : dataSnapshot.getChildren()) {
final Buildings buildings = postsSnapshot.getValue(Buildings.class);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Is this the correct way to implement it?
I used recyclerview, arraylist, and firebase to get and show data, but data is added to the list too many times..
This is my code
database=FirebaseDatabase.getInstance();
mRef=database.getReference("SETS");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot==null) return;
for(DataSnapshot userSanpshot : dataSnapshot.getChildren())
{
if(userSanpshot.child("DETAIL").getValue()==null) break;
single.Detail=userSanpshot.child("DETAIL").getValue().toString();
/~/
list.add(single);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter.setItems(list);
and this is my code when I send data to firebase
Button sharebut=(Button)findViewById(R.id.usersend);
sharebut.setOnClickListener(
new FloatingActionButton.OnClickListener() {
#Override
public void onClick(View view) {
mDatabase.child("SETS").child(userId).child("DETAIL").setValue(detailText.getText().toString());
/~/
finish();
}
}
);
}
Try Using this method
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// do here your work
}
#Override
public void onCancelled(DatabaseError databaseError) {
// handle error here
}
});
i wanto get all color data from firebase then insert to arraylist, and the color data structure like image below
and here is when i want to insert to Arraylist
private void getDataDetailProduct() {
imageReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot childSbapshot : dataSnapshot.getChildren()){
String id = childSbapshot.getKey();
ArrayList<String>images = new ArrayList<>();
images.add(String.valueOf(childSbapshot.getChildren()));
}
initSlideImage();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
ArrayList<String>images = new ArrayList<>();
private void getDataDetailProduct() {
imageReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot childSbapshot : dataSnapshot.getChildren()){
String id = childSbapshot.getKey();
images.add(String.valueOf(childSbapshot.child("colors1").getValue()));
images.add(String.valueOf(childSbapshot.child("colors2").getValue()));
images.add(String.valueOf(childSbapshot.child("colors3").getValue()));
}
initSlideImage();
}}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I have a textview in MainActivity.java. There are 5 strings (sentences) which are stored in firebase database. What I want is every time the MainActivity.java launches, a new string should be fetched from the database and must be displayed in the textview. But it only fetches the first string. I am not sure how to make this work! Is there any workaround? Any Help is appreciated.
MainActivity.java
public class Gridshow extends AppCompatActivity{
TextView myText;
private DatabaseReference database;
#Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
myText = (TextView) findViewById(R.id.Attri);
database = FirebaseDatabase.getInstance().getReference("StringList");
database.child("String1").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Sample son file:
{
"StringList" : {
"String1" : "Valley Rovers",
"String2" : "John Murphy",
"String3" : "Hello all",
"String4" : "See you all",
"String5" : "Hey all"
}
}
The problem is that you're directly saying to FB to retrieve just the first string in here:
database.child("String1").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You should retrieve the whole list and randomly choose the new string:
ArrayList<String> stringList = new ArrayList<>();
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> entries = (Map<String, String>) dataSnapshot.getValue();
stringList.addAll(entries.values());
selectString(stringList);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
public void selectString(ArrayList<String> list){
Random ran = new Random();
int x = ran.nextInt(list.size());
myText.setText(list.get(x));
}
use this
database = FirebaseDatabase.getInstance().getReference().child("StringList");
database.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Well, in this line:
database.child("String1").addValueEventListener
You are fetching just String1. You should instead use something like this:
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(int i = 0;i<dataSnapshot.getChildren().size();i++){
if(i==positionOfStringYouWant){
String sentName = dataSnapshot.getValue(String.class);
myText.setText(sentName);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I was trying to retrieve the data from user and time. But I can't access the data, it's not really accessing the database.
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
mFirebaseDatabase=FirebaseDatabase.getInstance();
cashRef=mFirebaseDatabase.getReference().child("ASSETS").child("cash_at_bank");
TextView view=(TextView)findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cashRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int time=dataSnapshot.getValue(int.class);
int values =dataSnapshot.getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
To get the time, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cashAtBankRef = rootRef.child("ASSETS").child("Cash at bank");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Long time = ds.child("time").child("time").getValue(Long.class);
Log.d("TAG", time + "");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
cashAtBankRef.addListenerForSingleValueEvent(eventListener);
The output will be:
150555043995
//and so on