Get UI Element ID In a Fragment From Custom adapter - java

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?

Related

RecyclerView inside PagerAdapter

I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)

Passing ArrayList from Fragment to Activity and Using BottomNavigationView

I am having the following problem, my application has two fragments, one with a Spinner where it contains the contents of the ArrayList and another fragment containing a ListView, I want to pass the contents of the Spinner ArrayList to the Activity to send it to the other fragment, I am doing so because I'm using a BottomNavigationView. Any idea how to do it?
your Fragments have context. This context could be casted to your Activity.
in Fragment
((YourActivity) getContext()).send(contents);

My listview values are reloading when i swipe one fragment to another in android?

i am using a tabHost for 4 fragments in this fragments i am calling custom adapters for each fragment when i move from one fragment to another fragment my list view is reloading freshly i dont want to refresh my listview from moving one fragment to another
Help me guys
Make your data object the member of your activity that is initializing the fragments.
Check this out. I would recommend using a ViewPager with a tabbed indicator, or a set of custom buttons linked to the pager. I've implemented this solution in several apps where each fragment contains heavy content, and the performance was great. Create all your fragments, and setup the pager on each change, this way the content of your "tabs" will stay the same on user switching.

Update private ListFragment from parent activity

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.

Clicking on a ListView

I have a ListView with a header and a footer. I added two items with a class of my own extending the ArrayAdapter class. In this extended class, I have overridden the getView function because I want Views which are not TextViews to appear in my ListView.
On this ListView, I have set an onClickListener.
The problem is that this onClickListener is started when I click either on the header or on the footer, but never when I click on my items.
Of course, the View returned by getView is set to be clickable.
What am I missing?
Do you want to do something different when each list element is clicked? If so, you should programmatically set an onClickListener for each of those 2 elements added. Can you post your code?
It sounds like you are making this much more complicated than necessary. You can create your own layout for each row of a ListView using an XML file. This allows you to use any combinations of Views that you wish. To control what happens when the user clicks on a row in the ListView, your activity class should extend ListActivity and override onItemClickListener(). Alternatively, you can call setOnItemClickListener() on your ListView. For more details, I suggest that you read the Android ListView Developer Guide.
The solution I found : the click listener attached to the 'ListView' is only used for the clicks on the header and the footer. For my custom rows, in my ArrayAdapter's 'getView' function, I attached one listener to the 'TextView' and another to the 'ImageButton'.

Categories