Using latest Parse library v1.5.1
Thanks to the update now I can do:
ParseQueryAdapter<ParseObject> mAdapter = new ParseQueryAdapter<ParseObject>(MainActivity.this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
#Override
public ParseQuery<ParseObject> create() {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseObject.class);
query.fromLocalDatastore();
return query;
}
});
mListView.setAdapter(mAdapter);
Now I have some pinned objects and they appear correctly, but when I unpin them like so:
//Some ParseObject in the above adapter
object.unpinInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
if(e == null) {
//I beleive this would be the correct approach.
mAdapter.notifyDataSetChanged();
}
}
});
Naturally I want that item to disappear from the corresponding ListView, but it doesn't. But say I go back to a different activity and revisit this activity, the ListView is displayed properly without the recently unpinned object.
Is this a bug? If not what am I doing wrong?
I have the same problem) I solve it with invoke method ParseQueryAdapter.loadObjects().
You can try mAdapter.remove(object) before calling notifyDataSetChanged();
unpinInBackground removes the object from the database. Probably the adapter has a local copy of the object.
Looks like there is no remove method in ParseQueryAdapter.
Here is an response from official source:
Since a ParseQueryAdapter is designed to always show the results of a
ParseQuery, you would need to use an API request to reload the query.
https://www.parse.com/questions/delete-a-object-using-parsequeryadapter
Related
currently I am using FirebaseRecyclerAdapter to represent data on a RecyclerView using the following Firebase query:
postsQuery = mDatabase.child("lists_new”).orderByKey().limitToFirst(10);
My RecyclerView has a header with 2 buttons: New List, Old List.
New list is loaded by default, and my question is, when the user taps the Old List button, what is the most efficient way to replace the new list with old list.
The Firebase query for the old list looks like this:
postsQuery = mDatabase.child("lists_old”).orderByKey().limitToFirst(10);
Note: both new list and the old list has the same data types, i.e. they share the same Java POJO class and the the same layout.
You will need a new adapter and attach that to the same RecyclerView.
So after constructing the new query, you create a new adapter and attach it to the view:
adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
Chat.class, android.R.layout.two_line_list_item, ChatHolder.class, postsQuery) {
recyclerView.setAdapter(adapter);
I have a similar need and a similar solution except I am calling cleanup() on the adapter before new'ing up another one. I am thinking without calling cleanup() it will create a leak of adapters and/or listeners?
In onActivityCreated() in my Fragment I am calling a method in the Fragment that manages the recycler view. Call the method to initialize or refresh the list and pass in a leaf node name. If the adapter is not null then call cleanup(). Create a new database reference by concatenating a new leaf node with the parent reference, new-up a new adapter and set it.
I call cleanup() in onDestroy() as well, per usual.
It works fine so far though I've only tested using the emulator and a small data set.
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
refreshList(newLeafNode);
}
#Override
public void onDestroy() {
super.onDestroy();
mRVAdapter.cleanup();
}
protected void refreshList(String newLeafNode) {
if (mRVAdapter != null) {
mRVAdapter.cleanup();
}
DatabaseReference newDbRef = mParentRef.child(newLeafNode);
mRVAdapter = new FirebaseRecyclerAdapter<String, MyViewHolder>(String.class, R.layout.single_row, MyViewHolder.class, newDbRef) {
#Override
protected void populateViewHolder(MyViewHolder viewHolder, String model, int position) {
viewHolder.setImage(model);
}
};
mMyRV.setAdapter(mRVAdapter);
}
My AsyncTaskLoader is loading data from a remote server. When new data arrives, naturally a call is made to onLoadFinished. At this point I don't know how to give the new data to the RecyclerView.Adapter. Calling notifyDataSetChanged does not give it the new data: it simply tells it there is new data. So any advice on how I might do this? Right now the best I can think of is to create my own setData method in my implementation of RecyclerView.Adapter as
public void setData(List<MyObject> data){
if(null != data && !data.isEmpty()){
synchronized(mItems){
mItems.clear();
mItems.addAll(data);
}
notifyDataSetChanged();
}
}
Is my idea the best there is? Or is there a more sound way of doing this?
Expose a public method in your adapter to update data.
For example, you could put it like this
public void updateItems(ArrayList<MyObject> myObjects) {
this.data = myObjects;
notifyDataSetChanged();
}
You have two options,
1. Re- instantiate your adapter with your new data and reset the adapter.
2. The way you do it.
I can not think of any other methods.
Ok, I had the same issue and here is the solution what I did.
1st I passed list object from onLoadFinished and in the RecyclerViewAdapter I have created method name setCardInfoList() and there I passed this object to global List object which I define in adapter class.
In onLoadFinished method..
#Override
public void onLoadFinished(android.content.Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
if (earthquakes != null && !earthquakes.isEmpty()) {
adapter.setCardInfoList(earthquakes);
adapter.notifyDataSetChanged();
}else {
emptyView.setText("No Earthquake Found...");
}
}
Inside Adapter class
public void setCardInfoList(List<Earthquake> earthquakes){
this.earthquakeList = earthquakes;
}
I'm using Parse.com as my backend and while there seems to be a method, saveInBackgroundWithBlock, to prevent duplicate entries. It doesn't appear to exist on Android. I'd like to upload only unique entries but can't figure out a way to do so.
The only thing I can think of is to query then insert if the entry doesn't exist, but that's doing twice as many network calls and I feel like it needs to.
Thanks
As I had mentioned in the comment earlier, I had faced the same problem. Ended up writing a query to find the existing objects and then save only the non-existing ones. Like below.
//Say you have a list of ParseObjects..this list contains the existing as well as the new objects.
List<ParseObject> allObjects = new ArrayList<ParseObject>();
allObjects.add(object); //this contains the entire list of objects.
You want to find out the existing ones by using the field say ids.
//First, form a query
ParseQuery<ParseObject> query = ParseQuery.getQuery("Class");
query.whereContainedIn("ids", allIds); //allIds is the list of ids
List<ParseObject> Objects = query.find(); //get the list of the parseobjects..findInBackground(Callback) whichever is suitable
for (int i = 0; i < Objects.size(); i++)
existingIds.add(Objects.get(i).getString("ids"));
List<String> idsNotPresent = new ArrayList<String>(allIds);
idsNotPresent.removeAll(existingIds);
//Use a list of Array objects to store the non-existing objects
List<ParseObject> newObjects = new ArrayList<ParseObject>();
for (int i = 0; i < selectedFriends.size(); i++) {
if (idsNotPresent.contains(allObjects.get(i).getString(
"ids"))) {
newObjects.add(allObjects.get(i)); //new Objects will contain the list of only the ParseObjects which are new and are not existing.
}
}
//Then use saveAllInBackground to store this objects
ParseObject.saveAllInBackground(newObjects, new SaveCallback() {
#Override
public void done(ParseException e) {
// TODO Auto-generated method stub
//do something
}
});
I had also tried using beforeSave method on ParseCloud. As you may know, before saving the objects this method is called on the ParseCloud and is ideal to make any validation required. But, it didn't quite run well. Let me know if you need something from the ParseCloud code.
Hope this helps!
I'm not sure I understand your question, but you can get the same functionality as saveInBackgroundWithBlock in Android like this:
myObject.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
myObjectSavedSuccessfully();
} else {
myObjectSaveDidNotSucceed();
}
}
});
I am trying to refresh a ListFragment after Asynctask is finished. In my Asynctask doInBackground method I connect to a database and add elements to an ArrayList. In the onPostExecute method I want to access my ListFragment by something like this to call it's refreshData(method):
//REFRESH ARTISTFRAGMENT
#Override
public void onPostExecute(ArrayList<String> result) {
ArtistFragment artistFrag = new ArtistFragment();
artistFrag = (ArtistFragment) artistFrag.getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.frame_container);
if (artistFrag != null) {
artistFrag.refreshData(result);
}
}
But getSupportFragmentManager results in a NullPointerException!
The refreshData method in my Fragment looks like this:
public void refreshData(ArrayList<String> data) {
artists = new ArrayList<String>(data);
this.artistAdpater.notifyDataSetChanged();
}
I have found very similar approaches to do the exact thing I want but I can't find a solution to my problem. Basically it's done over here https://stackoverflow.com/a/16388650 - but it doesn't work for me like that.
Has anybody a solution for this or a workaround?
I am assuming that artists is the array list that feeds your listview.
artists = new ArrayList<String>(data);
What you are doing creates a new arraylist object reference to artists. But the list adapter has the older refrence. Thats why the list is not getting update.
Instead of passing the reference of the new arraylist, you can add it to the old one like so:
newArtistsArraylist = new ArrayList<String>(data);
artists .add(newArtistsArraylist );
Here is a nice solution to your problem.
For my Android app, I have subclassed ParseObject to create Food, Pref classes and CustomUser class which extends ParseUser and created a relation "lovers" between 'Food' and 'Users'.
Inside a CustomUser object, I have stored an Pref object using
createWithoutData method in key "pref".CustomUser and its
respective Pref object have one-to-one mapping
So when I want to display all lovers of a particular food in a listview using ParseQueryAdapter,
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(getActivity(),
new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseQuery<ParseObject> query = food.getRelation("lovers").getQuery();
query.selectKeys(Arrays.asList("pref");
return query;
}
});
adapter.setTextKey(Pref.COLUMN_PROFILE_NAME);
adapter.setImageKey(Pref.COLUMN_PROFILE_PIC_THUMB);
fyi, COLUMN_PROFILE_NAME = "profileName", COLUMN_PROFILE_PIC_THUMB = "profileThumb"
Now the problem is that "pref" is only a reference to the actual object. So when the listView tries to get text and image, it says "ParseObject has no data for this key. Call fetchIfNeeded() to get the data"
My objective is to pass a query to ParseQueryAdapter that will fetch all pref objects nested inside CustomUsers having 'lovers' relation with that particular food.
The parse docs say that 'include' method does not work on relations.
Please help, I have been struggling on this for long now.
To retrieve a relation, I don't believe you can use a query. In your case you would use
ParseRelation<ParseObject> relation = user.getRelation("lovers");
See this documentation
Hope this helps
Below, an example showing off the level of configuration available with this class:
// Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this
// Adapter.
ParseQueryAdapter.QueryFactory<ParseObject> factory =
new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Customer");
//query.whereEqualTo("activated", true);
//query.orderByDescending("moneySpent");
query.include("your key");
return query;
}
};
// Pass the factory into the ParseQueryAdapter's constructor.
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
adapter.setTextKey("name");
// Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
public void onLoading() {
// Trigger any "loading" UI
}
public void onLoaded(List<ParseObject> objects, ParseException e) {
// Execute any post-loading logic, hide "loading" UI
}
});
// Attach it to your ListView, as in the example above
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);