I'm using Firebase and this line of code results in an error.
#Override
public void onCancelled(FirebaseError firebaseError) {
}
The errors are as below,
Method does not override method from its superclass
Method 'onCancelled(com.firebase.client.FirebaseError)' is never used
How to resolve these errors?
My code -
public class FirebaseClient {
Context c;
String DB_URL;
ListView listView;
Firebase firebase;
ArrayList<Dog> dogies= new ArrayList<>();
CustomAdapter customAdapter;
public FirebaseClient(Context c, String DB_URL, ListView listView)
{
this.c= c;
this.DB_URL= DB_URL;
this.listView= listView;
Firebase.setAndroidContext(c);
firebase=new Firebase(DB_URL);
}
public void refreshdata()
{
firebase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getupdates(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getupdates(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void getupdates(DataSnapshot dataSnapshot){
dogies.clear();
for(DataSnapshot ds :dataSnapshot.getChildren()){
Dog d= new Dog();
d.setTimestamp(ds.getValue(Dog.class).getTimestamp());
d.setInstituteNotice(ds.getValue(Dog.class).getInstituteNotice());
dogies.add(d);
}
if(dogies.size()>0)
{
customAdapter=new CustomAdapter(c, dogies);
listView.setAdapter((ListAdapter) customAdapter);
} else
{
Toast.makeText(c, "No data", Toast.LENGTH_SHORT).show();
}
}
}
You are using the legacy Firebase SDK (which has been deprecated for more than a year) and mixing in classes from the new SDK. You should consider migrating to the new SDK. The Upgrade Guide outlines the steps.
In the code you posted, DatabaseError is from the new SDK, not the legacy SDK, so this override is not valid and should be removed:
#Override
public void onCancelled(DatabaseError databaseError) {
}
In order to solve this, you need to remove the following method from your code:
#Override
public void onCancelled(FirebaseError firebaseError) {}
You are already overriding public void onCancelled(DatabaseError databaseError) from its superclass. The code that i adviced you to be removed was used in ealier version of SDK. You need to use the new one.
Related
I am facing a problem with adding older data to RecyclerView with SwipeRefreshLayout so, here is how i retrieve from Firebase and put data into RecyclerView
private void messageList() {
referenceMessages.orderByKey().limitToLast(10).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessagesInfo messages = dataSnapshot.getValue(MessagesInfo.class);
result.add(messages);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
MessagesInfo messages = dataSnapshot.getValue(MessagesInfo.class);
result.remove(messages);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
as you can i see, I set limit of retrieved messages to last 10, what means the 10 newest messages will be displayed. So far everything works great, the messages are displayed like that
-20thmessage(the oldest)-
-21stmessage-
-...-
-29thmessage-
-30thmessage(the newest)-
now i use SwipeRefreshLayout like that
private void swipe(){
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
referenceMessages.orderByKey().endAt("-LHUhkub4iaZNNzmtUcs").limitToLast(10).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessagesInfo messages = dataSnapshot.getValue(MessagesInfo.class);
recyclerView.scrollToPosition(0);
result.add(0, messages);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
and it retrieves data properly, i mean previous 10 messages before hardcoded endAt() but after swiping places data not properly. If i dont use index 0
new data is placed at the botoom of latest message and it looks like that
-30thmessage(the newest)-
-10thmessage-
-...-
-19thmessage-
-20thmessage(the newest)-
and when i use index 0 data is added from the top of already loaded messages but like that
-20thmessage(the oldest)-
-19thmessage-
-...-
-11ththmessage-
-10th-
so it is added upside-down. Hope you understand me :) any inputs are highly appreciated
You might try clearing the list first and then add the new messages.
Android SwipeRefreshLayoutBasic
I am trying to update adapter(MyCollegePagerAdapter) from CollegeHTMLParse.class. However, even though I created an object and called the notifyDataSetChanged() method on it, noting is showing up on the fragments. Can you tell me if there is anything wrong with my code as I have googled for solution for weeks now and after trying for hours upon hours, I am not able to figure out the issue.
Here is the CollegeHTMLParse class:
public class CollegeHTMLParse extends AppCompatActivity {
public static ArrayList<String> colleges = new ArrayList<>();
public static ArrayList<String> accepted = new ArrayList<>();
public static ArrayList<String> link = new ArrayList<>();
public static ArrayList<String> location = new ArrayList<>();
public static ArrayList<String> tuition = new ArrayList<>();
FirebaseDatabase mRef;
DatabaseReference database;
public static ArrayList<String> data = new ArrayList<>();
MyCollegePagerAdapter adapter = new MyCollegePagerAdapter(getSupportFragmentManager());
public void onCreate(String CollegeCode) {
colleges.clear();
accepted.clear();
link.clear();
location.clear();
tuition.clear();
mRef = FirebaseDatabase.getInstance("https://successway18.firebaseio.com/");
database = mRef.getReference().child("CollegeCodes").child("30");
database.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
setCollegeValues(dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setCollegeValues(String values) {
colleges.add(values);
int i = 0;
while (i < colleges.size()) {
database.child(colleges.get(i)).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
setDetailsValues(dataSnapshot.getValue().toString());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
i++;
}
}
private void setDetailsValues(String details) {
data.add(details);
if (data.size() == 4) {
accepted.add(data.get(0));
link.add(data.get(1));
location.add(data.get(2));
tuition.add(data.get(3));
data.clear();
}
update();
}
private void update() {
adapter.notifyDataSetChanged();
}
}
Thank you very much in advance!
Add this in your pager adapter
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Firebase query runs in the worker thread, so you have to run notifyDataSetChanged in the main thread.
Try to use the below code in the function update()
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
// Toast.makeText(Map.this, marker.getPosition()+"", Toast.LENGTH_SHORT).show();
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Toast.makeText(Map.this, "test", Toast.LENGTH_SHORT).show();
dataSnapshot.getRef().removeValue();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
});
Why am I not able to delete the marker?
onChildRemoved() -- no response.
The problem is that you're adding a value change listener, so onChildRemoved() will only be called once you remove the child, but you're removing it in the callback function.
Try it like this:
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) { child.getRef().removeValue(); }
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
The query returns a collection of datasnapshots where there is a match. Then you'd need to iterate over them deleting every one of them.
I need to retrieve single data from Firebase database. Where I have a list view contains id list. Whenever user clicks the listview i need to display the respective value field.
check screen shot of db
here is the code.
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
r2= FirebaseDatabase.getInstance().getReference().child("type").child("discord").child("ad");
r2.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String name1;
name1=dataSnapshot.getValue().toString();
Toast.makeText(Main2Activity.this, name1, Toast.LENGTH_SHORT).show();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
I need to execute the query
select value from table_name where id="01" ;
You need to change this line of code:
name1=dataSnapshot.getValue().toString();
with
name1=dataSnapshot.child("01").getValue(String.class);
Change the addChildEventListener to addValueEventListener.
For example:
r2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name1 = dataSnapshot.child("01").getValue(String.class);
Toast.makeText(Main2Activity.this, name1, Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
Reference
I want to Delete Specific value from Firebase Database following is Database which i have stored
Now, I want to Delete Following Values
fname: "John"
lname: "wick"
Here, What i have try to delete specific value from Firebase..
DatabaseReference demo = mReference.child("demo").orderByValue().equalTo("John").getRef();
demo.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this will delete all values from database...
Question : How to delete that specific value??? what i have to do for that
Any help will be appreciated.
Instead of using addListenerForSingleValueEventtry using addChildEventListener as below,
Query queryRef = mReference.child("demo").orderByChild("fname").equalTo("John");
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
snapshot.getRef().setValue(null);
}
});
hi try using this code,
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
You can try it:
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});
Well Thanks to all, After Modification with lots of answer and query.. This is what worked with me.
mReference.child("demo").orderByChild("fname").equalTo("John").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
//also work
//dataSnapshot.getRef().setValue(null);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this code. Hopefully this will help you
Thanks
database.getReference().child("Groups").child(groupItem.getGroupkey()).child("members").child(groupMember.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Customlog.showlogD("ERROR_MSG",e.getMessage());
}
});
Database structure: https://i.stack.imgur.com/Lw3Ia.png