Passing data between tabbed fragment lists - android

I have implemented:
public class CategoryListFragment extends ListFragment
(Categories) Tab1
public class CrimeListFragment extends ListFragment
(List of crimes) Tab2
-- If you click on the tab it returns ALL crimes.
public class CrimeFragment extends Fragment
(Individual Crime) Tab 3
-- If you click on the tab it returns first crime.
And the activity that hosts all of the tabs:
public class TabsFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener
What I want to implement is an onListItemClick on (Tab 1) "Categories" that passes the ID or string to Tab 2 so that only the crimes of that category are displayed.

This is already described in the official android documentation.
please follow the link and read about it there: https://developer.android.com/training/basics/fragments/communicating.html

Related

ListFragment setListAdapter to custom adapter found outside fragment class

I have an activity with a layout, and the layout consists of a fragment that is a ListFragment. Insise my ListFragment derived class, I want to set the ListFragment from an adapter that is located inside the activity whose layout holds the fragment. Is there a way to pass the adapter to the fragment onActivityCreated, or do I need to create a new adapter inside the onActivityCreated method?
MainActivity.java --> adapter instance is here
activity_main.xml --> fragment is located in this viewgroup
MyListFragment.java --> I want to use setListAdapter(myAdapter) in onActivityCreated
If question doesn't make sense let me know. I am new to android and might have to reword.
Create one public method in activity:
public class MainActivity extends Activity{
public YourAdapter getYourAdapter(){
return adapterInstatnce;
}
}
In your ListFragment :
in onActivityCreate()
final YourAdapter yourAdapter = ((MainActivity)getActivity()).getYourAdapter();
You can pass just the adapter "data" from activity to fragment (use newInstance method), and the create the adapter inside the fragment.

Android ViewPager update list adapter from a different tabbed fragment

I have a View pager Fragment called SalesPartsFragmentHolder which has two child tabbed fragments
SalesPartsFragment
SelectedSalesPartsFragment
Both of these contains Lists with an adapter. What I want to happen is when an item is clicked on SalesPartsFragment, it is added to SelectedSalesPartsFragment. So, when a user moves to the other tab it is showing. I have tried
mAppSectionsPagerAdapter.notifyDataSetChanged();
but this is too slow, if it is possible to tell the list adapter in SelectedSalesPartsFragment or to get mAppSectionsPagerAdapter.notifyDataSetChanged(); to run on the click of a list item from within SalesPartsFragment would be great. Any ideas?/ thoughts?
You need create interface in SalesPartsFragment. Example
class SalesPartsFragment extends Fragment{
...
public interface onSalesPartsFragmentListener{
public void onItemClick()
}
private onSalesPartsFragmentListener mListener;
...
#Ovveride
public void onAttach(Activity activity){
mListener = (onSalesPartsFragmentListener)activity;
}
...
void onItemClick(){
mListener.onItemClick();
}
Activity must implements onSalesPartsFragmentListener and notify SelectedSalesPartsFragment when SelectedSalesFragment was clicked.
class MainActivity extends Activity implements onSalesPartsFragmentListener{
...
public void onItemClick(){
selectedSalesPartsFragment.update()
}
In SelectedSalesPartsFragment create method update() and call notifyDataSetChanged();

How to update the ListView of a Fragment from another Fragment?

How can I communicate a listview of a ListFragment after an event of a Fragment inside another Fragment?
In the ListFragment (fragment A) I have a method to refresh the ListView. But also, I need to refresh it after a click of a Button inside a Fragment, wich is child of another Fragment (fragment b)
Its like Fragment A (listFragment) | Fragment B (detailview)
(fragment C - child fragment of B)
How can I do it?
You can access another Fragment by its tag:
// find your fragment
YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");
// update the list view
f.updateListView();
The tag of your Fragment is set when it is attached to a container layout:
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();
So when you click your Button, find the Fragment you want to refresh by its tag, and then call your refresh method.
IF you are using a ViewPager, this is how to get the Fragments tag:
/**
* Gets the fragment tag of a fragment at a specific position in the viewpager.
*
* #param pos the pos
* #return the fragment tag
*/
public String getFragmentTag(int pos){
return "android:switcher:"+R.id.yourViewPagerId+":"+pos;
}
You can do it with a few simple steps:
Create a listener interface for component to listen to the button click event from FragmentC. For example:
public interface FragmentCButtonListener {
public void onButtonClicked();
}
Add a method in FragmentC to allow listener registration. FragmentC will keep track of the listener, and call the listener callback as soon as the button is clicked. For example:
public class FragmentC extends Fragment {
FragmentCButtonListener myListener;
public void registerListener (FragmentCButtonListener listener) {
myListener = listener;
}
}
FragmentA should implement FragmentCButtonListener interface, register itself as a listener to FragmentC, and refresh the list view when it receives the callback from FragmentC. For example:
public class FragmentC extends Fragment implements FragementCButtonListener {
FragmentC fragmentC;
public void onCreate() {
fragment = new FragmentC();
fragment.registerListener (this);
}
public void onButtonClicked() {
//refresh the list view
}
}
Please note, I assume the FragmentA has a reference to FragmentC in the sample. If not, just make sure the container class of all fragments registers itself as the listener of FragmentC. The container class can as FragmentA to update listview once it receives callback from FragmentC.
follow these steps
We have two fragments called AddFragmentand ListFragment, and upon adding an item on first fragment you want the updated list be shown on list fragment (what sort of sorcery is this!!!).
Step 1 create the listener interface on class level of AddFragment with a method that is going to be implemented by the other guy (ListFragment ) and create Interface type variable
public class AddFragment extends Fragment{
//listener varriable
//listener
public interface OnCategoryAddedListener{
public void refreshList();
}
private static OnCategoryAddedListener meAddListener;
}
Step 2 create register method on class level of the same AddFragment class and set listenter variable
public class AddFragment extends Fragment{
public void registerListener(OnCategoryAddedListener listener)
{
meAddListener = listener;
}
}
Step 3 upon any event cud be button click or yelling at ur application(that is considered rude event in android :-) ) check for listener object meAddListener variable and call the interface,
in a Shakespeare’s nutshell it means “for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. ”
Step 4 On ListFragment implement the AddFragment’s interface,no turning back just go implement its method. Within that method u just call abracadabra to repopulate ur list or any sort of updatable android view object… and ur done
public class ListFragment extends Fragment implements AddFragment.OnCattegoryAddedListener{
//refer to AddFragment
AddFragment addFragment;
//once the fragment is within the memory scope u instantiate AddFragment
//and register listener with AddFragment context which inherently implement OnCategoryAddedListener
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoryAddFragment = new CategoryAddFragment();
categoryAddFragment.registerListener(this);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fillList();
}
public void fillList() {
ArrayAdapter<String> catArrayAdapter = new ArrayAdapter<>(context,android.R.layout.simple_list_item_1,getItems());
setListAdapter(catArrayAdapter);
}
//le interface method u can count on to refresh ur list content
public void refreshList(){
fillList();
}
check this out for a little more
enjoy the java magic

call methods defined in the fragments from SherlockFragmentActivity

I am using SherlockFragmentActivity with two fragments.
I want to call methods defined in the fragments from my SherlockFragmentActivity.
Fragment1 extends Fragment{
protected void resetSettings(){
//my code
}
}
and the activity
Main extends SherlockFragmentActivity{
//here i want to call resetSettings()
}

Android ExpandableListActivity and TabActivity in same Class

I am trying to use the ExpandableListActivity in my program but I have already inherited the TabActivity in my class. I cannot do multiple inheritance and I need to use both the ExpandableListView and the Tabs in my program.
I checked Google's code for the ExpandableListView here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html.
But here they are inheriting the ExpandableListActivity which I cannot do as I have already inherited TabActivity in my class.
I am aware that I can use ListView without inheriting ListActivity in my class. Can I do the same for the ExpandableListView class also ? If so, then can anyone please provide me with any sample code to use ExtendableListView without inheriting the activity ?
You can have a custom view that extends ExpandableListView and use it inside the TabActivity.
Expandable List View
public class CustomExpListView extends ExpandableListView{
/** Your code **/
}
Main Activity that hosts the tabs
public class MyTabActivity extends TabActivity{
private TabHost tabhost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
tabHost.addTab(tabHost
.newTabSpec("Tab 1")
.setIndicator("Tab 1",
this.getResources().getDrawable(
R.drawable.tab_icon))
.setContent(new Intent(this, Tab1.class)));
/** Further code**/
}
}
XML Layout file for the tab with the exp list (Tab 1)
/**Other layout stuff goes here**/
<com.jmango.nexus.view.ProductListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:groupIndicator="#drawable/group_indicator"
android:childIndicator="#drawable/child_indicator"
android:cacheColorHint="#00000000"
android:divider="#00BBFF"
android:childDivider="#android:drawable/divider_horizontal_dark"
android:smoothScrollbar="true"
android:id="#+id/exp_list_view"/>
In the class for the tab with the exp list
CustomExpListView custExpListView = (CustomExpListView) this
.findViewById(R.id.exp_list_view);
You can also extend the listeners and customize it.
Hope it helps!

Categories

Resources