in Education by
I have a main activity which contains the action bar with 3 menu buttons in it. I then have a fragment within this main activity which has a list. I would like to be able to refresh the list in the fragment from the main activity, when one of the menu buttons is clicked, or preferably just removed all the rows from the list. Any help is appreciated. Thanks. public class Favourite extends SherlockFragmentActivity { ActionBar actionBar; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.favourite); actionBar = getSupportActionBar(); actionBar.setDisplayShowTitleEnabled(false); BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.actionbar_bg); bg.setTileModeX(TileMode.REPEAT); getSupportActionBar().setBackgroundDrawable(bg); getSupportActionBar().setIcon(R.drawable.favourite_title); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab tabAll = actionBar.newTab(); ActionBar.Tab tabfavs = actionBar.newTab(); ActionBar.Tab tabhist = actionBar.newTab(); tabAll.setText("all"); tabfavs.setText("favs"); tabhist.setText("hist"); tabAll.setTabListener(new MyTabListener()); tabfavs.setTabListener(new MyTabListener()); tabhist.setTabListener(new MyTabListener()); actionBar.addTab(tabAll); actionBar.addTab(tabfavs); actionBar.addTab(tabhist); try{ } catch(Exception e) { } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.actionbar_itemlist_favourite, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.history: break; case R.id.favourite: Intent favAct = new Intent(this, Favourite.class); startActivity(favAct); break; case R.id.delete: ///I WANT TO BE ABLE TO REFRESH FRAGMENTLIST FROM HERE } return true; } } class MyTabListener implements ActionBar.TabListener { public void onTabSelected(Tab tab, FragmentTransaction ft) { if(tab.getPosition()==0) { FavouriteAllWords frag = new FavouriteAllWords(); ft.replace(android.R.id.content, frag); } else if(tab.getPosition()==1) { FavouriteFavWords frag = new FavouriteFavWords(); ft.replace(android.R.id.content, frag); } else if(tab.getPosition()==2) { FavouriteHistWords frag = new FavouriteHistWords(); ft.replace(android.R.id.content, frag); } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } } ////////////////////MY LIST FRAGMENT CLASS public class FavouriteAllWords extends ListFragment { ArrayAdapter adapter; List stringOfFavWords; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { adapter = new ArrayAdapter( inflater.getContext(), R.layout.row, stringOfFavWords); setListAdapter(adapter); return super.onCreateView(inflater, group, saved); } @Override public void onActivityCreated (Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can easily achieve this using INTERFACE MainActivity.java public class MainActivity extends Activity { public FragmentRefreshListener getFragmentRefreshListener() { return fragmentRefreshListener; } public void setFragmentRefreshListener(FragmentRefreshListener fragmentRefreshListener) { this.fragmentRefreshListener = fragmentRefreshListener; } private FragmentRefreshListener fragmentRefreshListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b = (Button)findViewById(R.id.btnRefreshFragment); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(getFragmentRefreshListener()!=null){ getFragmentRefreshListener().onRefresh(); } } }); } public interface FragmentRefreshListener{ void onRefresh(); } } MyFragment.java public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = null; // some view /// Your Code ((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener() { @Override public void onRefresh() { // Refresh Your Fragment } }); return v; } }

Related questions

0 votes
    I have a Fragment1, and i am make newInstance of Fragment1 and start in Fragment1, i.e. my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have created different fragment and populated some menu based on the fragment. I was getting duplicate ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have the following codes in my android application: main.xml : fragmenta.xml : FragmentA.java : package ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    What is fragment in android?...
asked Jan 14, 2021 in Technology by JackTerrance
0 votes
    I am creating an application for Android. I am using Realm as a Database. Realm working fine in the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have an activity A that launches B with an Intent. (B is a MapActivity and has some async code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    In My Activity there are more then one ListView. See the XML Layout of it: Now i am able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    What is an activity in Android?...
asked Aug 13, 2021 in Technology by JackTerrance
0 votes
    How to launch an activity in android?...
asked Jan 14, 2021 in Technology by JackTerrance
0 votes
    I've got a menu with multiple child elements, the problem I'm having is that the child menu is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I've got a menu with multiple child elements, the problem I'm having is that the child menu is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I've got a menu with multiple child elements, the problem I'm having is that the child menu is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
...