single activity recyclerview for two button - android

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.

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

Refresh listview in fragment

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

Android: How to dynamically replace a list view with another list view like Instagram with onClick?

I'm building an android app and I'm trying to replace a list view with another list view if you click a button, like the notifications page on Instagram. On that page, if you click on the top "following" button it will show you a listview of what your followers have liked. If you click on the "you" button it will show you a listview of what people have liked your photos.
Any help would be greatly appreciated!
You can do it by following ways,
1. add Two listviews and can change the visibility as per your requirement.
2. On button click you can load the other data into same list view and can update your adapter in the same list view.
in 1 you have to load two list view at first which will consume more time if data is larger sure you can write a login in asynctask to load list views in background thread.
in 2 you have to update your adapter at the button so you will have to provide some progress bar of dialog for user while you list view is getting update.
You can use either of this whichever suites you best.
Simply , You don't need to switch Listview , you only need to switch adapters .
eg, you can switch to mFollowingAdapter when clicked on Following button and switch to mYouAdapter when you select "You" tab. that's it.
You should write a list, that has a custom adapter. This adapter will be able to display BOTH views you want to display.
If the data to be displayed is the same format (ie. both have an imageview next to a textview), you are in good shape.
When you want to switch to a different list, get the information you would like to display, replace the data in your the collection backing your list, then notify the list that the data has changed, and it should redraw.
So, this might look like:
create ArrayList() with data A
setup List, with this data and display
replace the ArrayList() with data B
call listView.notifyDataSetChanged
You can still do this if the Data A, and Data B have different views, in this case, you would need to handle this logic in your custom adapter.

Adding new listview to current listview item?

I'm newbie on android development and developing a app to improve myself.
My app is listing students from db with ListView. I want to do"When I click the name of student, it will show the exam grades" I mean,I want to add new listview to onclick of list item.
Is it possible? What I need to do that(in xml and code),Do you have any code sample?
Create a custom listview (http://www.vogella.com/articles/AndroidListView/article.html) Add a listview to the listview item and set its visibility to GONE. When they click the item set the visibility to VISIBLE.
Add an OnItemClickListener listener in the current ListView and when you get the click event query your data base for that particular student to get result. Once you have result you can load a new ListView in same Activity using Fragment or in a totally new fragment. (You are already using a ListView, so obviously you know how to load data in ListView).
If you want to show the grades in a separate screen, I would suggest that you'll create a new Activity with it's own layout (that will contain the grades ListView) and when a click on a student name will occur you will start the new Activity to display grades (you can pass the student id on the new Activity Intent).
You can also consider using Fragments. This way you will be able to change the layout to master-detail view (like in mail apps) easily on tablets. You can start by looking at this link.

loading listview twice?

I have an activity on the top of it there are some buttons from which when a button pressed it loads the dialog with country list and when user choose choose the country it will load the channel list below buttons.
so when activity start it shows blank screen until user choose the country
so i want to show a list when activity will start and then same procedure will follow as above.
but how can i load two different listview?
i tried
acivity start
load the default country channel's list
buttonclick listener
perform click {
load the another list
}
using the base adapter class found here.
Just switch adapter to current list (this is the easy way to do it)
OR
In your android xml file, where you create your activity, create two listviews with different ids. First listView will have android:visibility="visible" (this one will be shown first) and the second one will have android:visibility="gone"(you will make this visible when you perform the click).
When you want to switch the lists, just set first listview visibility to View.VISIBLE and the other one to View.GONE, from code.
Don't forget to switch adapters for different lists (this may be a bug source)
I would do following
Have a flag like isCountrySelected
Change your adapter and onitemclick handler so that according to isCountrySelected, you either load channel list or country list.

Categories

Resources