What is the best way to update ListView? - android

Good day,
I make my test application in Android and find some difficult moments to do by myself.
I have two Tab Fragments and Host Activity (that creates two tab fragments) with Action Bar (actionbarsherlock). One Fragment allows to add some information about product and persist it into SQLite. Other Fragment has a listview that displays all the products from database.
The problem is that in the first tab I persist data in different thread with 5 second delay, so you click "Add Product to Database" button and after 5 seconds it will be added. But right after clicking I can switch to another tab with listview and the situation is as follows: data from the first fragment is not persisted yet because of delay, but after it is persisted, nothing changes in listview - the listview doesn't know about changes. I need listview updated authomatically when it notices there are changes in database. How can I do this? I use ArrayAdapter to populate data into listview.

I think you need necessary use broadcast receiver, and in it call method refreshDrawableState() for you list. Respectively receiver is your activity.

Related

One or more fragments containing listview with tabs?

I have used Android Studio example with tabs and fragment, but I have some different idea. I need to automatically generate tabs and fragments, which will contain listview and will be populated using JSON.
I am successfully fetching data from JSON and placing them in one activity, but my intention is to use tabs which will serve as categories, so each time I click on different category it will show different news. My intention is that first time user clicks on tab/category, app should populate it with news, and next time he gets back it will not reload, it will show what is loaded before unless he wants to refresh it.
Refreshing category should be done by pulling down.
So, should I use one fragment as template, as all news will be presented the same way and populate it with data, or will I need to create fragment for each category?
If I will need to create different fragments for each, then in order to add new category I would need to update app version.
No need to create different fragment for each category. Just change the adapter content as you load data from server. But data for each category must be same ,means json object must have same keys in all categories. Otherwise use different fragments for each category of news.
Why dont you use pull to refresh on each tab fragment. And apply the condition, if pull to refresh then load new contents or refresh the data... or else display the old data as it is. Hope it helps you!

can it loads content after the tab is clicked? not creating the spec in one go

Now I want to create a TabHost for 3 categories, Pizza, Spaghetti and Snack. Information will be taken form database. Is it possible to access only information of that specific category after the tab is clicked instead of creating it when the activity starts. If it is possible, which way is better.
I made something like you over a week ago and after long debugging I realized that the system loads the present tab and the one after it at the same time in case to be ready for swiping anytime you wanna do it (I mean their layouts). So if you place your database logic on the Fragments not on your Activity directly (different database logic on the different tabs), when you load your app, only the first tab database logic will be loaded, but the second tabs layout will be loaded too. Hopefully you could understand what I meant :)

How to add 'listview rows' from fragment 1 to fragment 2 with SQL?

I have several custom list views set up inside each separate fragment. What is the way to save a specific list row from fragment 1 and immediately retrieve it into a dedicated (favorites) fragment? The user should also be able to delete (unfavorite) the list rows that have been populated in the favorites fragment.
And since each list row has a specific onItemClick action associated with them, will this stay consistent when the row is accessed from the favorites fragment?
After modifying the data in one fragment, update the data in your database, and tell the other fragments to reload the data. In order to do that register a BroadcastReciever in all fragments that listens to the action when the database have been updated. Maybe it is faster to do it directly between the fragments, but this way you can garantee the consecvency of the UI and the DB.

Update a ListView located in a different tab

I'm a newbie Android developer and I'm trying to create an Android application with 2 tabs, using Activities for each tab. This is basically how it should work:
Tab A has 1 button
Tab B has 1 ListView
When the user clicks on the button in Tab A the application must put a value in the ListView in Tab B
My question is: how can I update the ListView in Tab B when I click on the button in Tab A? I know how to put values in a ListView when it's on the same tab where the button is located but my approach doesn't work when the ListView is in a different tab. I tried...
ListView myListInTabB = (ListView)findViewById(R.id.list_on_tabB);
but didn't work :-/
Any suggestion?
Thanks in advance.
Im not sure you actually need seperate activites for this, unless you are doing some specific work between your activities. Most tab solutions you see in Android solutions work simply by hiding the Views that are not related to the current shown tab. You will find the "visibility" property helpful for making this happen. Also, making sure all of your Views are in the same Activity lets you access their objects all the time, regardless of their visibility.
You don't do that. Just ensure the underlying Adapter has the latest data. Calling requery() on the Cursor does the trick, it automatically updates the ListView attached to it.
Edit: use notifyDataSetChanged() to let the list know that the data is stale.

Should I use Activities or Views with tabs & lists?

I'm new to Java & Android development. I'm trying to develop an app that has 2 tabs, 1 tab has a listview inside of it. When you click on an item in the list, it takes you to another list.. and then you select another item, onto another list, ect.. until they reach the final page which I have setup as a non-selectable list. My question is.. should I create a new activity every time the user clicks on an item in the list? Or is that something normally done with changing views? If done with views, doesn't that pretty much disable their use of going back with the back button?
In the other tab I have an area with a list I guess in which you can remove items off of the list.. Now would I create a new activity for this and put this tab activity on every activity of the lists? I guess this part is what got me confused.. if I hadn't had the other tab my current setup of creating a new activity as the user drills down the lists worked just fine.
This all might sounds a little confusing but let me know if you guys need further explanation..
I would use changeable views only when the views are conceptually showing the same data from a different viewpoint. Since you say "it takes you to another list", I'd say use a separate activity.
As for the tab, my understanding is that you can model each tab as a separate activity, I'm not clear on why you would "put this tab activity on every activity of the lists"? Are you saying that one tab (the "remove" tab) is dependent on what's showing on the other tab ("the list tab")? Without knowing more about the context, but first instinct would be to use a separate "remove tab" activity and model the tab host as having separate activities per tab.

Categories

Resources