I have a FragmentActivity which creates a ListFragment. Inside the ListFragment is a custom adapter. When I would try to make the adapter public, it wouldn't let me. So, I don't really have access to the adapter or I'm not sure how to get it. I add some data to the listview statically with a public arraylist of objects that I declare early in the lifecycle. However, other data I want to retrieve with AsyncTask from a Database. I get this data in the parent activity and I know right when it is finished fetching the data. So,
Can I update the listView within the ListFragment with access to the arraylist, but not access to the adapter? If so, can I do it from the activity or do I need to somehow call a method in listFragment? If I need to call a method in listFragment, how do I access that method from the activity? If this is not possible without access to the adapter, how do I get access to the adapter without the public keyword? Would it be better to move my AsycTask to my ListFragment?
I know it sounds like a lot, but I'm just wondering how to refresh the listview with those cicumstances. Help is greatly appreciated.
Call to method:
ListFragment fragment = (ListFragment) getSupportFragmentManager().findFragmentByTag("fragtag");
fragment.updateData();
Method that refreshes:
public void updateData(){
((ArrayAdapter)this.getListAdapter()).notifyDataSetChanged();
}
It is in my subclass of ListFragment, but not in any methods.
**Edit:**Now I get the following error:
Error parsing data java.lang.ClassCastException: com.dynamic_internet_solutions.scsinfo.app.SpeakerListFragment$1SpeakersListAdapter cannot be cast to android.widget.ArrayAdapter
**Edit - Solved: ** I did
ListFragment fragment = (ListFragment) getSupportFragmentManager().findFragmentByTag("fragtag");
BaseAdapter adapter = (BaseAdapter) fragment.getListAdapter();
adapter.notifyDataSetChanged();
after I set the tag of my fragment on initialization.
You always have access to the ListAdapter of a ListFragment using the getListAdapter() method. You can easily grab the list adapter and modify the data in it. I'm not sure why you are having trouble making your adapter public. Are you declaring it inline, or as a separate class? What is the error you are getting from the compiler?
As an aside, you might want to read my two articles on the View Holder pattern and making reusable list adapters to make your ListAdapter more awesome.
Related
suppose i have an app that has a tool bar and a fragment container in the main activity, the fragment container in this situation is displaying a fragment that has a recyclerview which has a list of objects retrieved from a basic room database, and the toolbar has a button that clears out all the data stored in the database (which means the code that responsible for this is located in the main activity not in the fragment), how should i go about notifying the adapter inside the fragment that the object list has been updated?
i don't think any code is necessary here as it's more of a general question, but i will gladly share all the code necessary if its needed.
You can make the adapter a public static variable and call notifyDataSetChanged from the main activity
I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?
I have one Tabbed Activity which has 3 Fragments. In every Fragment I need to display some data from my realtime firebase database. So i thought that instead of connecting to database in every Fragment, I would retrieve data to static variable inside my tabbed activity, and then display this data calling this variable from every Fragment. But when I try to set my text to data from Firebase it shows an error, because in that moment, my static variable is "null". How do I make sure that that first I retrieve the data to my variable and then set my text. Because right now, my tabbed activity has ValueEventListener inside the onCreate method, and I am trying to set the TextView, from OnCreateView inside Fragments. I made some tests and realized, that the onCreate method inside of Tabbed Activity is called first, but onCreateView from my Fragment is called shortly after when the data isn't retrieved yet.
You can simply get Reference of your fragments like below
ExampleFragment myFragment = (ExampleFragment)getSupportFragmentManager().findFragmentByTag(ExampleFragment.TAG);
And then when the data finished loading. Send it like below code.
myfragment.sendDataToFragment(myData);
i hope it helps.
Also you can use interfaces to communicate between Activity and fragments.
I'd like to implement a simple Search Function in my App.
I already implemented all the Layout Related things and it works without problems.
Now, I'd like to find Items from a ListView using the Search Function.
The Problem is, that my ListView is not in the same Class. When i try to access my ListView from the other Class, i keep get a NullPointerException.
This is what i have in my MainActivity:
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText))
{
// new otherActivity().listView.clearTextFilter(); NullPointerException
}
else
{
// new otherActivity().listView.setFilterText(newText.toString()); NullPointerException
}
return true;
}
In my otherActivity, i do have ListView listView; outside the onCreate() Method and listView = (ListView) findViewById(R.id.listView); inside the onCreate() Method.
I also tried to put the whole ListView outside the onCreate() Method but that ended up in a NullPointerException aswell.
There are some possible ways that you can use ListView in a class that have its reference.
1.) Pass reference of ListView in other class constructor
2.) create a static getter method of ListView in your Activity having ListView
3.) Use Application class to setter and getter of ListView instance
4.) Another way is have an instance of Activity with as static getter method and
use that to get instance of your ListView in another Class.
These above mentioned possible ways may vary depending on your requirement
new otherActivity()
You are not supposed to create an Activity in this way.
Use an Intent instead to start your Activity.
Suppose that I have a main layout that has a ListView. This ListView has a custom cursor adapter. When my list change, is there anyway to know, in the main activity, if my list has been changed?. Inside my custom cursor I call to this.notifyDataSetChanged();. Is there any method that can I use to know when notifyDataSetChanged() has been called?.
You need to create a DataSetObserver and register it with your adapter by calling registerDataSetObserver().
p.s. I strongly suggest that you familiarize yourself with the Android API Reference. This is a great tool to help you find the classes and methods which are available in the Android API.