Update a ListView located in a different tab - android

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.

Related

Clearing OffscreenPage in Android ViewPager

I have a ViewPager and multiple tabs.
One of these tabs controls the settings, which affects every other tabs.
Everything updates itself perfectly fine when I change the settings, except for the pages that are kept in memory. In order to get these to update I have to select a tab far enough from it, then come back.
Is there a way to force these tabs to recreate themselves?
Thank you
According to this question on SO, you can reset the adapter which will reload all the pages:
viewPage.setAdapter( adapter );
The duplicate that that question is linked to also gives some helpful information, such as calling .notifyDataSetChanged() on your ViewPager adapter.

What is the best way to update ListView?

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.

android-adding tabs in view flipper

I am developing an android application in which I have creted a TabActivity. For each tab I am using a separate activity and a separate layout file. Actually this activity is a details screen which shows customer information. The user can click on an item in a listview activity in order to see customer's detailed info in the tab acticity. There he can use navigation buttons to navigate through the customers In a few words, if the listview displays 10 records and the user clicks on the first item the customer details.tabactivity opens and displays detailed info. Using the navigation butons the user can see the next or previous record.
Now, I would like to use a viewflipper in the details screen in order to use animations while navigating through the records and in the end. to use fling gestures instead of buttons. Nevertheless I haven't found a proper example of how to add a tabhost/tabactivity in the flipper. I also thought to create the tabs using one layout in order to add it to the viewflipper but then I have no way to create the tabs inside the activity that hosts the viewfliper.
Any help would be apreciated. Thank you in advance.
ps. I will create a basic app of what I am talking and upload if that would be helpful
The ViewPager may be more appropriate for what you are trying to do.

How to close one single tab in android tablayout without clearing all the tabs and adding again

There is a method to add a tab in TabHost as well as clear all tabs, but there are no APIs to remove one single tab.
Following the logic in clearAllTabs tried to
tabwidget.removeViewAt(index);
tabHost.getTabContentView().removeViewAt(index)
After this the behaviour is strange. I assume that is because the mTabSpecs still contain the tabspec reference. mTabSpecs is a private variable in TabHost and there are not get methods to get a handle to this.
How to resolve the issue if one wants to close a single tab, and yes I have tried to clear all the tabs and add back all the tabspecs. It does not work for my usecase where some views contains some information regarding a session. If I recreate those tabspecs, I will go back to the starting point in those views.
You can try to set Visibility of the Tab that you want to close to View.GONE.
resolved with the View.GONE but with method which specifically checks for the visibility before counting the tabs. drawback is the index of the view cannot be changed.

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