Refresh listview in fragment - android

In my android application, I have one spinner box and two edit text box for adding item.When I click on add button all the data will store in database and display in below Item list (listview display in my first image) by refreshing listview.
My problem is, when I click on add button at a time only one list is display and listview heading is also gone.
May be this problem is raised due to refreshing of listview.
I searched on google and found notifyDataSetChanged() method is used for refreshing list view but I don't know how notifyDataSetChanged() method is used in fragment?
Does anybody know how to use this method in fragment?
I am new to fragment. In my app all the java code is developed using fragment activity, and xml file is simple layout.(LinearLayout/RelativeLayout)
You can see my screen here:
1)First image show list heading.
2)Second image show List item.

You need to setAdapter again to your listview after adding new data. it will show new data int listview.
if not get, post your code where you setAdapter to listview.

You have to first add item in Arraylist/List which you passed in Adapter then after you can call below code..
adapter.notifyDataSetChanged();
listView.invalidateViews();

Related

How can I know if data in the Listview has changed in android?

I have an unsolved problem:
what I want to do: I have an activity with a TextView which shows the mathematical sum of all items: example. The Listview contains several items which have an amount (double). I want to show the sum of all items inside the activity.
Generally, it works fine when the activity runs the first time, but if I add an item to the list (with a button) later on it is shown correctly inside the list. but I have to update the Textview in the activity. how can I do that, because I don't know a function which tells me that da dataset has changed?
what works actually:
the activity, the sum calculation, and the Listview (with custom
adapter).
In the Listview there is a checkbox, when it is changed a dialog is opened where the new amount is inserted. (that is all done in the adapter)
everything is correctly shown in the listview after a change
What should be solved:
Now, as soon as the dialog box is closed, the sum in the activity (which is outside of the Listview) has to be updated. but how do I get that information back to my activity?
Thank you for your support!
Best regards
Jason
The ListView is in the mainactivity and works fine. In the custom Adapter I can change some date of the Item (each line). Now the Data and ListView is updated and correctly shown. But not The TextView in the mainactivity.
I solved it now by passing the TextView object to the Custom Adapter and do the setText(....) there

single activity recyclerview for two button

I created a player list program, now according to the program I created, there is a button (at start_activity) when the program starts. clicking on this button will open a list created by the next "recyclerview". I have a question about how to put two buttons (to "start_activity") ("new stars" and "historical stars") and connect both to one recyclerview, the list design will be the same for both, only the names in the list will change . is it possible to use the same recyclerview for both to make it much simpler? how please show an example from the code. Thank you.
It's simple.
On first button click - get your list data & set an adapter in a recyclerview.
On second button click - get your other list data & set an adapter in the same recyclerview.
So your data will be updated. You just have to initialize new data & adapter on button click, then set adapter in the recyclerview.

Android ExpandableListView save input in each item's edit text on button click

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.

List focus on a particular item in that list dynamically

In my project i have a ListView for handling the list of a product. In that i have a list of 100 Items in that List view. Each and Every time i have been seeing the first row of the ListView..
If i need to see the data in the ListView Means, i need to scroll down, Can i focus directly to a particular item by any function by passing the position(ID) of the ListView..
For Example this is my ListView that contains 11 items in it..
I'm seeing every time the ListView as the following,..
And my try is, If i give the ListView position as ID=5 means, can i get the Focus of the ListView as follow, in the opening itself...
Can we get this by ListFragment method or anything else?
Suggest me for the Best..
try this, use setselection
listview.setSelection(position);
Don't know about ListFragment but in listview, you can use listview.setSelection(position);
This method will help you to achieve this
for example: listview.setSelection(5);

Android - updating the simpleCursorAdapter

I have a ListActivity which gets its data from a database query. I also have a custom adapter which extends simple cursor adapter.
To show the customised content, I have overridden the newView and bindView methods.
Each element of the view has:
TextView containing a title
ImageView containing number of stars - the image shown is changed based on a value obtained from the database
A button - the button text changes on clicks (Favourite/Make Favourite), and triggers a database update event.
My problem is this - when I scroll the ListView, the changes I made seem to disappear .. for instance, the first item is marked favourite, and the list is scrolled ... when I come back to the first item, the text reverts back to its previous value, although internally the db has been updated.
I read that the notifyDatasetChanged() is not right for this case as the adapter should be notified of the database changes. I am trying to use the reQuery() method, but do not know where to place it.
Should I place the reQuery() in the onClick method of the button? If not, where should it be placed?
What so ever Adapter is you are using must have getView Method.. Whenever a list is scrolled this getView Method got called for all the listItem in the view. Make Sure in the getView Method. That the view is getting generated dynamically.
It should check for clicked or changed value.
to update the list without scrolling your have to call listView.getAdapter().notifyDataSetChanged()
Check the following link. here i mentioned a complete code to use listview properly. Using this we can achieve any listview behavior. We can embed animation also.
Change ListView background - strange behaviour
Hope this help :)
If you have extended BaseAdapter ,
after data is changed call , listView.setAdapter() and in getView() , update the view's data too.
Use SetListAdapter with ArrayAdapter to display the list. Use ConvertView to avoid inflating View.
if (convertView==null)
{
//inflate your list
}

Categories

Resources