I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.
Related
I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.
I have a view pager with two tabs in my app (A and B). The first tab i.e A has a listview. Clicking on any of the items in the list opens a new fragment (let's call it C) with another list. The tab B also has a listview.
The list in tab B and the list contained in the fragment C have the same type of items. That is each item of both list is of the same class. I have used a recycler view with a custom adapter to create the list. Since both lists in tab B and fragment c are of the same object type I have used the same adapter, namely (CarListAdapter.class) for this purpose.
The problem I am facing is that when I open fragment C then go back to tab A and then right swipe so that tab B is visible, clicking on an item in tab B causes null pointer exception, reason being that the list in fragment CarListFragment.class still has the data from fragment C and not tab B.(However the list is rendered properly, it is only the click that is not working properly)
Since two instance of this adapter are present I expected this to work fine. If however I use two different adapters with exact same code like (CarListAdapterC.class and CarListAdapterB.class) the code works fine.
I am stuck, can someone please explain why this is happening?
I have encountered same problem when using view pager and recycler view. I have searched allot and as i know it's an issue in recycler view adapter.
As you may know view pager load one more next page by default every time it shows a page.and here is where adapter shows the problem.The different instances of a same adapter that you create point to same place.because of that it still has other pages items.
I know it's not a good idea but i have created another adapter same as the first one with different name and used it for the next list and it worked.try to separate your list's adapters.let me know if it works.
I finally found the solution, it was really stupid on my part. I had static fields in RecyclerView Adapter. Removing them worked like a charm.
I have a listview, clicking on one of the item in listview takes me to some fragment (different item may take to different fragment). Once a new fragment is opened I want to swipe to open the the next item in list instead of going back and clicking on the next item. I am thinking of using ViewPagers but not sure how to open the next item in the list.
You can create an Activity which has a ViewPager and Fragments with desired info as its children. On a list item click, take the position info and pass it to the Activity via Bundle. And inside the Activity after setting the ViewPager, you can show the desired Fragment with ViewPager's setCurrentItem(int item) function.
I'm making a simple notepad app. In one fragment there's a list view of added notes and when some note is selected the other fragment shows the details of that note. Now if user clicks delete button a note is deleted, but how do I refresh the list view and detail view accordingly?
For refreshing the ListView actually what you have to refresh is the underlaying Adapter.
This would be the workflow of your scenario:
Remove the selected item
Call notifyDataSetChanged()
Set another item as the current one, for example the previous note or the first one.
And that's it.
I have three ListFragment is one activity. For the first listFragment I have created a ListFragment and inflate a layout with some listItem easily:
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources.getStringArray(R.string.baal)));
The item of the second listFragment will change according to the selected item of the first ListFragment and in the similar way The item of the third listFragment will change according to the selected item of the second ListFragment.
The first ListFragment is static as it won't depend on any other ListFragment. So I simply inflate a layout with some item for it.
But The items of second and third listFragments need to be changed frequently. So I need to inflate it in run-time with new listitems every time on click event. So I think the second and third LisFragment needs to be create dynamically by inflating every-time with new list items. How can I achieve that? I am new in Fragment and I need my concept on dynamic UI clear. Thanks in advance.
You can, for example, try to clear your adapters of second and third fragments and then add all necessary items, this way you would not need to inflate the whole fragments, but instead only the content would change. Also depending on adapter types you would need to call notifyDataSetChanged or not (if it is ArrayAdapter for example).
UPD: On your main item click event you can write some code like this
YourAdapter1 adapter1 = getFirstAdapter();
adapter1.clear();
adapter1.addAll(newItem1Collection);
YourAdapter2 adapter2 = getSecondAdapter();
adapter2.clear();
adapter2.addAll(newItem2Collection);
where YourAdapter1 and YourAdapter2 should extend ArrayAdapter, this will also automatically invoke notifyDataSetChange.
I suggest you use tabs, adding tabs dynamically for each selection, and allowing the user to swipe back to cancel a selection, something like breadcrumbs navigation.
You can see the related question here:
Android - How to create tabs on demand using existing layout?