Updating a ListFragment based on Async Task result - android

Scenario :
I am having a list fragment which displays a list of friends. When an list item is pressed, I'm showing the details of the friend in a dialog fragment.
Problem:
In the dialog fragment, I'm having an option to delete the friend from my friend list (in a remote server) . I'm using Async tack to perform this. After returning from the dialog fragment, how should I update the 'old' friend list. How do I trigger a fragment 'reload'. Since the async task is not aware of the custom list adapter being used in the list fragment.
Sorry, I will add the code ASAP. If anyone have met this scenario before kindly provide your suggestions.
Edit: After successfully deleting a friend from the friend list in a remote server, I want to reload the list being displayed in the list fragment. Is there a way to trigger the dataSetChanged notification in the list fragment itself.

So you will be populating the ListView with an ArrayList.
Delete the Item from the ArrayList with index = ListViewItemIndex
clicked.
call notifyDataSetChanged on the Adapter you are using.

No problem. I'm guessing the ListFragment is reading from some data source - be it an ArrayAdapter or a SimpleCursorAdapter (or something similar) - and your task is also modifying that source. When the AsyncTask finishes, you should just be able to get that fragment and update the underlying data. For example:
// ...
void onPostExecute(Void result) {
ListFragment lFrag = (ListFragment) getFragmentManager().findFragmentById(R.id.yourListFragment);
BaseAdapter adapter = (BaseAdapter) lFrag.getListAdapter();
adapter.notifyDataSetChanged();
}

Related

android - ArrayAdapter not updating, but Sqlite Database updated

I am working on restaurant app.
In that, i have cart activity.
That, I manage with the database. When the user adds an item, i insert it to a database.
I created one class that extends ArrayAdapter
in this class, i display data of each item in the cart.
when user finalizes cart items, i post it to WebService.
Problem
in this cart, itemi have a button "remove this item."
when I click on it, item is deleted from the database but not
reflecting on ArrayList.
adapter.notifyDataSetChanged() not working.
NOTE
when I go back to that activity and come, it will be removed.
Just remove the item from Arraylist.
yourArraylist.remove(poistion);
Then you notify to adpter changed by below code
adapter.notifyDataSetChanged();
Try this it may help you.....

How to update the particular list item in list view in android

I'm new to Android. I have an application where some items appear in a list view. Now I want to add some new items. I want to show the word "New" for newly-added items, and when a new item is clicked that "New" word should disappear. How should I do this?
Follow that answer that is use for gridview you can also same for listview, notifyDataSetChanged();
Android Gridview is not refreshed while pressing back button
Update the data backing your list adapter and call notifyDataSetChanged() on the adapter.
On selecting particular item from the list, you can change the status in the model class of that item ,from new to empty string "". And then depending on this value you can set the visibility of view if you want.
As the model in the datalist is updated you need to notify your adapter that underlying data has beeen changed. So call notifyDataSetChanged() on the adapter.
UPDATE:
notifyDataSetChanged() -
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
When an Adapter is constructed, it holds the reference for the List that was passed in. If you pass-in a List that was a member of an Activity, and change some of the data later, the Adapter is still holding a reference to the original List. The Adapter does not know you changed the List in the Activity. Hence call notifyDataSetChanged() on Adapter.
Try following code.
listview.notifyDataSetChanged();
listview.invalidateViews();

need to reload mainActivity controller on click of button located in customized listview

To make my question more understandable let me start with an image of my view.
I have an xml file named Menu, that has customized list view in it. I have created another xmlview named MenuCell as below.
Now tapping on add button I'm adding Item to the cart. which is working perfectly fine except not updating value of a cart (top right corner) on click event. But If I navigate to different view and come back to this view at this point I'm getting number of items added in the cart reflected properly. So How Can I reload my controllerview when I tap in my arradepter view's ImageButton.
this is my adapter code
holder.imageButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addItem(position);
notifyDataSetChanged();
}
});
void addItem(int position) {
count++;
}
Where count is item added count.
If anyone can tell How am I able to reflect this count of my arrayadpter class to my other controller class that holds actual list view.
Any Help will be appreciated
Thanks in advance.
You need to use callback feature to get notified to your activity , hence add button is part of list component so you can update your list view.
Here are few links for understanding of call back using interface
http://cleancodedevelopment-qualityseal.blogspot.in/2012/10/understanding-callbacks-with-java.html
http://stackoverflow.com/questions/11577695/what-is-a-call-back-interface-in-java
After :
notifyDataSetChanged();
just add:
YourList.invalidateViews();
YourList.scrollBy(0, 0);
Send the reference of your controller to your list adapter class. In the controller I guess you have a method that computes some data and after that call update the view that holds cart data summary. Just make sure that update is on UI thread.
Your List is not refreshing at the instant because you have to refresh data of your adapter and then call notifyDataSetChanged();
Suppose You have the data in an array which is visible in textview.Then In your function addItem add the data into ur array and then call notifyDataSetChanged();
This will immediately tell the list view that watever data it is containing is changed so time to refresh.
If you can paste more of ur adapter code i can be helpful

Activity list view display different data (String array) on click of listitem

I have an activity which contains listview, when click on any list item, i want to display same activity with listview with different data and so on.
When i click on back button i need to display same activity listview with old data.
Is it possible? or is there is any other way to achieve this. I dont want to create any new activities or fragments for this?
Thanks
KrishIndia
You can write the function such that each time you click an item, it generates the list, assigns the data to your adapter and attaches it to your list view of choice.
If fetching the data won't be expensive for you, you could use only one adapter (array adapter or list adapter or simple adapter should be fine) and just re-assign it each time. (Ex. If you were listing files in a directory, inside your onClick() function you would have some function declaration like list_items(directoryName) that you would call each time. That function would declare and set an adapter for your data.)
If you are concerned about the expense of re-fetching the data, just store multiple adapters (outside your onClick() function) and set them to your list view as needed.
on click of list item just set new adapter or new data and call notifyDataSetChanged()
and for backpress override onBackPressed() and handle accordingly
Maintain separate adapter for each new data. When you want to return back just pass the relevent adapter object to that list view

Android listview update without refreshing startactivity

I'm developing an Android app which looks like Facebook. FeedsActivity.java has a ListView which show user's feeds. When the ListView item is clicked, it starts the activity ViewActivity.java
ViewActivity shows one targeted feed. If user clicks the like button in ViewAcitivity, like count will be updated and then if the user clicks the back button, FeedsActivity will be shown.
I want to update ListView item's like count without refresh.
Here is my logic
FeedsActivity.java
onCreate(){
// http request to get json feeds
// parsing json
// setAdapter
}
setOnItemClickListener(){
// intent.putExtra() // FEED DATA
// startActivity
}
ViewActivity.java
public void goBack(View v){
// intent.putExtra() // FEED DATA INCLUDE UPDATE LIKE COUNT
// startActivityForResult()
}
When I call startActivityForResult() in ViewActivity.java, onCreate() of "FeedsActivity.java" is called so my ListView gets updated. (request json feeds and setAdapter) :(
I want to update only like counts, such as Facebook app.
Is there any other good way?
please help me.
have a look at notifyDataSetChanged() of your adapter. this method basically tells the adapter that the provided data has changed and that it should update the ListView. so this will makes your ListView refresh and display new data

Categories

Resources