android - ArrayAdapter not updating, but Sqlite Database updated - android

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.....

Related

ListView pass data to another ListView

I have two activities with List View's:
Activity A --> ListView person
Activity B --> ListView favouritePerson
In person ListView there is a button i every item of the list
person --> Button likeButton
I would like to add person to favouritePerson list when the like button is clicked. I was trying to achieve this by adding data to singleton and then trying to get it from there, but without success. I have also tried to use SharedPreferences, but could not get it right either. What would be the best approach for this workflow?
I also forget to mention, that I'm populating the first listView by querying the Realm database.
I would like to add person to favouritePerson list when the like button is clicked
To get this, onClick like button, you need to write a query to insert favourite bit in data base against that record. And when you load activity B updated data will be populated. No singleton, sharedpreference or Parcelable is required. This is the best approach. Update database record on the fly and get updated data in Activity B.

Display db content in another acitivity by clicking on item in listview

So I have this problem, my listview is populated from Sqlite DB and I can click on any item in the listview. When I click on an item I want to display, in a new activity these "name, surname, age etc" details but these should be retrieved from my database.
Can anyone help me?
Thank you.
Use onItemClickListener method in your listview. When user clicks an item pass the item into next activity and in next activity retrieve data based on the item click. This tutorial will help you to get a good idea.
Import and Use External Database in Android.
Set an onItemClickListener for your ListView and when an item is clicked, you could either:
Place into a Bundle the details you want to be given to the Activity intent
Or
Place as an extra/bundle to the Activity intent the ID of the item you retrieved, then retrieve the details you want from the DB using the given ID inside of the next Activity

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();

Updating a ListFragment based on Async Task result

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();
}

How to get a selected contacts?

I'm new to Android. I have two activities. First activity shows selected contact list from second activity. Second activity shows contact list from database with checkbox. Now I can read a contact list from database and shows at checkbox. Therefore I don't know how to pass selected contacts from first to second activity and how to display selected contact list. Is there any easy way to do it?
Thank you for every advice and guide.
Thanks,
Zeck.
First you have to get Data from your database
then create a CursorAdapter or any Adapter you want and
fill it with data and override getView() there.
Here http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
and here http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
Set your adapter into a ListView then
implement onclicklistener on this ListView
where you will pass the data to Bundle and start a new Activity
listView onclick goes to a new activity
Here
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
and here
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
There're many other ways how to share the data between activities
like SharedPreferences or Sqlite database. But it's not the topic for this answer.
hope it helps abit

Categories

Resources