I Have 4 Fragments in 1 Activity. I need to make sure that on every fragment change the fragment is refreshed because I load JSON Data in every fragment. I use FragmentPager Adapter and RecyclerView to Show Data
This is my Adapter ( SickAdapter.java ) the code :
public class SickAdapter extends FragmentPagerAdapter{
private static final String TAG = SickAdapter.class.getSimpleName();
private static final int FRAGMENT_COUNT = 4;
public SickAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SickFragmentToAll();
case 1:
return new SickFragmentToPending();
case 2:
return new SickFragmentToApproved();
case 3:
return new SickFragmentToDenied();
}
return null;
}
#Override
public int getCount() {
return FRAGMENT_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "All";
case 1:
return "Pending";
case 2:
return "Approved";
case 3:
return "Denied";
}
return null;
}
}
In my Class ( SickClass.java ) I create SickClass and I Call SickAdapter, I explode them, and this is my Code :
public class SickClass extends Fragment {
private static final String TAG = SickClass.class.getSimpleName();
private SickAdapter mAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
public SickClass() {
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.employee_leave_class, container, false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.view_pager);
if(mAdapter == null) {
mAdapter = new SickAdapter(getChildFragmentManager());
}
viewPager.setAdapter(new SickAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1, true);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
}
Try below code for reload your fragment;
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(YOUR_FRAGMENT_NAME.this).attach(YOUR_FRAGMENT_NAME.this).commit();
You can use startActivityForResult() to get the result from the activity.
References:
ViewPager PagerAdapter not updating the View
How to refresh a Fragment of a FragmentPagerAdapter?
Related
I'm new at Android programming and I need your help. Please.
What I want to do.
I created listview, from listview I created OnItemClickListener to TabbedActivity.
Now I want for each listview item to show different text on TabbedActivity.
This is MainActivity:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
listview = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.bolesti));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
startActivity(intent1);
break;
}
}
});
listview.setAdapter(mAdapter);
}}
TabbedActivity:
public class TabbedActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tabbed, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
Tab1Opis tab1 = new Tab1Opis();
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
TabbedActivity contains 4 tabs, all of them have different .java class example:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
return rootView;
}}
I need to change text in every fragment when I click on different listview item.
Can you help me? Thank you.
Check the pictures. Thank you a lot.
You can pass text from your MainActivity to TabbedActivity fragment Tab2Simptomi as below:
1. Pass text from MainActivity to TabbedActivity, using Intent extras.
Update onItemClick() as below:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
intent.putExtra("YOUR_TEXT", "Text One");
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
intent1.putExtra("YOUR_TEXT", "Text Two");
startActivity(intent1);
break;
}
}
});
2. Pass text from TabbedActivity to Tab2Simptomi, setting bundle as arguments of fragments inside SectionsPagerAdapter.
In TabbedActivity onCreate() method, get text that was passed from MainActivity
public class TabbedActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Get text
String text = getIntent().getStringExtra("YOUR_TEXT");
// Pass text to SectionsPagerAdapter
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), text);
.............
......................
}
}
Update SectionsPagerAdapter to pass text to Fragments:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String yourText;
public SectionsPagerAdapter(FragmentManager fm, String yourText) {
super(fm);
this.yourText = yourText;
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("YOUR_TEXT", yourText);
switch (position){
case 0:
Tab1Opis tab1 = new Tab1Opis();
tab1.setArguments(bundle); // Pass text to Tab1Opis fragment
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
tab2.setArguments(bundle);
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
tab3.setArguments(bundle);
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
tab4.setArguments(bundle);
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
3. Finally get text from individual Frgament and do whatever you want.
Update Tab2Simptomi as below:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
String finalText = getArguments().getString("YOUR_TEXT");
// Do something with finalText
Toast.makeText(getActivity(), "Final Text: " + finalText, Toast.LENGTH_SHORT).show();
return rootView;
}
}
UPDATE:
Assuming your fragment texts are static.
1. Pass texts from MainActivity to TabbedActivity, using Intent extras.
Update onItemClick() as below:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch (i){
case 0:
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
intent.putExtra("OPIS", "Item1 :: Text for Fragment OPIS");
intent.putExtra("SIMPTOMI", "Item1 :: Text for Fragment SIMPTOMI");
intent.putExtra("UZROCI", "Item1 :: Text for Fragment UZROCI");
intent.putExtra("LIJECENJE", "Item1 :: Text for Fragment LIJECENJE");
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(MainActivity.this, TabbedActivity.class);
intent1.putExtra("OPIS", "Item2 :: Text for Fragment OPIS");
intent1.putExtra("SIMPTOMI", "Item2 :: Text for Fragment SIMPTOMI");
intent1.putExtra("UZROCI", "Item2 :: Text for Fragment UZROCI");
intent1.putExtra("LIJECENJE", "Item2 :: Text for Fragment LIJECENJE");
startActivity(intent1);
break;
}
}
});
2. Pass texts from TabbedActivity to Tab2Simptomi, setting bundle as arguments of fragments inside SectionsPagerAdapter.
In TabbedActivity onCreate() method, get text that was passed from MainActivity
public class TabbedActivity extends AppCompatActivity {
ArrayList<String> stringArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
stringArray = new ArrayList<String>();
// Get texts
String textOPIS = getIntent().getStringExtra("OPIS");
String textSIMPTOMI = getIntent().getStringExtra("SIMPTOMI");
String textUZROCI = getIntent().getStringExtra("UZROCI");
String textLIJECENJE = getIntent().getStringExtra("LIJECENJE");
// Add text to ArrayList
stringArray.add(textOPIS);
stringArray.add(textSIMPTOMI);
stringArray.add(textUZROCI);
stringArray.add(textLIJECENJE);
// Pass ArrayList to SectionsPagerAdapter
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), stringArray);
.............
......................
}
}
Update SectionsPagerAdapter to pass text to Fragments:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<String> stringList;
public SectionsPagerAdapter(FragmentManager fm, ArrayList<String> stringArray) {
super(fm);
this.stringList = stringArray;
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("YOUR_TEXT", stringList.get(position)); // Get string from stringList using position
switch (position){
case 0:
Tab1Opis tab1 = new Tab1Opis();
tab1.setArguments(bundle); // Pass text to Tab1Opis fragment
return tab1;
case 1:
Tab2Simptomi tab2 = new Tab2Simptomi();
tab2.setArguments(bundle);
return tab2;
case 2:
Tab3Uzroci tab3 = new Tab3Uzroci();
tab3.setArguments(bundle);
return tab3;
case 3:
Tab4Lijecenje tab4 = new Tab4Lijecenje();
tab4.setArguments(bundle);
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Opis";
case 1:
return "Simptomi";
case 2:
return "Uzroci";
case 3:
return "Lijecenje";
}
return null;
}
}}
3. Finally get text from individual Frgament and do whatever you want.
Update Tab2Simptomi as below:
public class Tab2Simptomi extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_simptomi, container, false);
String finalText = getArguments().getString("YOUR_TEXT");
// Do something with finalText
TextView yourTextView = (TextView) rootView.findViewById(R.id.your_textview);
// Show text on TextView
yourTextView.setText(finalText);
Toast.makeText(getActivity(), "Final Text: " + finalText, Toast.LENGTH_SHORT).show();
return rootView;
}
}
Hope this will help~
In MainActivity
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(MainActivity.this, TabbedActivity.class);
switch (i){
case 0:
intent.putExtra("Title", "Title1");
break;
case 1:
intent.putExtra("Title", "Title2");
break;
}
startActivity(intent);
}
});
In TabbedActivity,String title is Title1 or Title2 based on what was clicked in MainActivity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
String title = getIntent().getStringExtra("Title");
Toast.makeText(this, ""+title, Toast.LENGTH_SHORT).show();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.v(TAG, "onCreateView");
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
// setting up the view pager and tab layout
mViewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
mTabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
PageAdapter adapter = new PageAdapter(getFragmentManager());
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
return rootView;
}
My custom pager adapter looks like this:
public class PageAdapter extends FragmentStatePagerAdapter {
public PageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new GroupTabFragment(position);
}
#Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "Game"; // this is from a template, i have to
break; // return title dynamically
case 1:
title = "Movie";
break;
}
return title;
}
#Override
public int getCount() {
return 2; // the tabs number is a subject of change
} // i don't know it from the begining
}
The problem i have is that i have to add or remove a tab when a button is clicked, or something like this.
Each tab will display a classic list of objects.
Thank you.
There are 2 ways doing this although ViewPager is not suitable for dynamic removing:
1) Pass ArrayList<Fragment> data as parameter to PageAdapter
Then Adapter will look like this:
public class PageAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> data;
public PageAdapter(FragmentManager fm, ArrayList<Fragment> data) {
super(fm);
this.data = data;
}
#Override
public Fragment getItem(int position) {
return data.get(position);
}
#Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "Game"; // this is from a template, i have to
break; // return title dynamically
case 1:
title = "Movie";
break;
}
return title;
}
#Override
public int getCount() {
return data.size(); // the tabs number is a subject of change
} // i don't know it from the begining
}
to remove item you do in Activity\Fragment:
data.remove(fragmentId);
adapter.notifyDataSetChanged();
2) Recreate adapter every time you want to delete item. So you basically create new adapter with new data.
data.remove(fragmentId);
PageAdapter adapter = new PageAdapter(getFragmentManager(), data);
mViewPager.setAdapter(adapter);
Instead of using mTabLayout.setupWithViewPager(mViewPager); you can setup your tabs manually:
mTabLayout.addTab(mTabLayout.newTab().setText("Movie"));
mTabLayout.addTab(mTabLayout.newTab().setText("Game"));
mTabLayout.addTab(mTabLayout.newTab().setText("Audio"));
mTabLayout.setOnTabSelectedListener(this);
Removing is also easy: removeTabAt(int position) or removeTab(Tab tab)
Here I have found some better solution to remove all the views / fragments from the current context.
Here I have used TabLayout and ViewPage within Fragment.
So we need to clear tabs from TabLayout, views from ViewPage and fragments from FragmentManager.
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
FragmentManager fragmentManager = null;
public TabLayout tabLayout;
public ViewPager viewPager;
public ViewPagerAdapter(FragmentManager manager, TabLayout tabLayout, ViewPager pager) {
super(manager);
this.fragmentManager= manager;
this.tabLayout = tabLayout;
this.viewPager= pager;
}
public void removeAll() {
tabLayout.removeAllTabs();
viewPager.removeAllViewsInLayout();
viewPager.removeAllViews();
viewPager.setAdapter(null);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
if (manager.getFragments() != null) {
manager.getFragments().clear();
}
viewPager.setAdapter(adapterPage);
viewPager.invalidate();
viewPager.requestLayout();
mFragmentList.clear();
mFragmentTitleList.clear();
}
I have a App with a Slidingmenu where i can pick different Fragment, which displays different kind of ListFragments.
The ListFragments will be filled by JSON from my Database Server.
If you Click on one of the Listitems, a new Fragment is shown which contains more Details about the selected Listitem.
public void updateList(final Activity a, final ListFragment L) {
adapter = new SimpleAdapter(a, mCommentList,
R.layout.single_comment, new String[] {TAG_PIC_ID,TAG_CATEGORY, TAG_ACTIVITY, TAG_DATUM, TAG_AKTUSR, TAG_MAXUSR, TAG_GENDER, /*TAG_POST_ID,*/ TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME }, new int[] { R.id.imgrow, R.id.category, R.id.activity/*R.id.id*/ , R.id.datum, R.id.aktusr, R.id.maxusr, R.id.gender, /*R.id.category,*/ R.id.title, R.id.message,
R.id.username });
L.setListAdapter(adapter);
ListView lv = L.getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int intid = (int)id;
Details nextFrag= new Details();
L.getFragmentManager().beginTransaction()
.replace(R.id.frame_container, nextFrag, "Test")
.addToBackStack(null)
.commit();
}
});
}
When i call the function from a "single" fragment its no problem and the DetailPage is shown.
The Problem is when i call the function from a TabbedActivity with a SectionsPagerAdapter.
The TabbedActivity
package info.androidhive.slidingmenu;
public class TabbedActivity extends Fragment {
SectionsPagerAdapter mSectionsPagerAdapter;
public static final String TAG = TabbedActivity.class.getSimpleName();
ViewPager mViewPager;
public static TabbedActivity newInstance() {
return new TabbedActivity();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_item_one, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getChildFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return v;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new newEntrys();
switch (position) {
case 0:
fragment = new newEntrys();
break;
case 1:
fragment = new oldEntrys();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Aktuelle Einträge";
case 1:
return "Vergangene Einträge";
}
return null;
}
}}
The TabbedActivity contains the 2 Fragments newEntry and oldEntry which are nearly equal.
package info.androidhive.slidingmenu.fragments;
public class RegisterdEvents_new extends ListFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.read, container, false);
return rootView;
}
public void startTask(){
Read SO = new Read();
Load LEvt = SO.new Load(getActivity(), this, "NEW");
LEvt.execute();
}
public void onResume() {
startTask();
super.onResume();
}
}
So the function updateList above is called in
Load LEvt = SO.new Load(getActivity(), this, "NEW");
But the problem is that i always get the Error
No view found for id 0x7f0a000f (package:id/frame_container) for fragment Details{e3992e2 #0 id=0x7f0a000f Test}
Someone of you can help me to solve this problem?
This is my page:
package[...]
import [...]
public class Home extends FragmentActivity {
ViewPager mViewPager;
ListView list;
row_video_Adapter adapter;
public Home CustomListView = null;
public ArrayList<ModelloLista> CustomListViewValuesArr = new ArrayList<ModelloLista>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// ListView
CustomListView = this;
/******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/
setListData();
Resources res =getResources();
list= (ListView)findViewById(R.id.section_label); // List defined in XML ( See Below )
/**************** Create Custom Adapter *********/
adapter=new row_video_Adapter(CustomListView, CustomListViewValuesArr,res);
list.setAdapter(adapter);
}
/****** Function to set data in ArrayList *************/
public void setListData(){
final ModelloLista sched = new ModelloLista();
sched.setTitolo("Video 1");
sched.setImmagine("video_preview");
sched.setUrl("http:\\www.com");
CustomListViewValuesArr.add(sched);
}
/***************** This function used by adapter ****************/
public void onItemClick(int mPosition){
ModelloLista tempValues = ( ModelloLista ) CustomListViewValuesArr.get(mPosition);
// SHOW ALERT
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 5 total pages.
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
[...]
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_dummy,
container, false);
//TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
switch(getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:
//dummyTextView.setText("text 1");
break;
case 2:
[...]
}
return rootView;
}
}
}
How can I put my Custom ListView in a fragment?
I tried to move the code in the switch(getArguments().getInt(ARG_SECTION_NUMBER)){ but there were many errors.
What should I do?
I have app with FragmentStatePagerAdapter. One of the fragment contains some linearlayouts. I also have ActionBar. When i click at icon(button) on ActionBar, I will update my ArrayList with strings. After update ArrayList i want to update LinearLayout in fragment.
This is my Java code of FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter{
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position) {
case 0: {
Fragment fragment = new NejblizsiBary();
Bundle args = new Bundle();
args.putInt(NejblizsiBary.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
case 1: {
Fragment fragment3 = new Mapa();
Bundle args = new Bundle();
args.putInt(Mapa.ARG_SECTION_NUMBER, position + 2);
fragment3.setArguments(args);
return fragment3;
}
case 2: {
Fragment fragment2 = new OblibeneBary();
Bundle args = new Bundle();
args.putInt(OblibeneBary.ARG_SECTION_NUMBER, position + 3);
fragment2.setArguments(args);
return fragment2;
}
default:
return null;
}
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section3).toUpperCase(l);
case 2:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
Here is my class with creating content of Fragment:
public class NejblizsiBary extends Fragment implements LocationListener {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public NejblizsiBary() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
LinearLayout mainLayout = (LinearLayout) rootView.findViewById(R.id.hl);
getApplicationContext();
LayoutInflater inflater_layout = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i < pocet_baru;i++){
View itemBox = inflater_layout.inflate(R.layout.jedenbar,null);
itemBox.setId(i);
TextView nazev = (TextView)itemBox.findViewById(R.id.nazev);
nazev.setText(bary.get(i).getNazev());
View.OnClickListener handler = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Detaily dialog = new Detaily();
Bundle args = new Bundle();
int id = arg0.getId();
args.putDouble("hlat", mLatitude);
args.putDouble("hlong", mLongitude);
args.putDouble("lat", bary.get(id).getLatitude());
args.putDouble("long", bary.get(id).getLongtitude());
args.putString("nazev", bary.get(id).getNazev());
args.putString("adresa", bary.get(id).getAdresa());
args.putString("mesto", bary.get(id).getMesto());
args.putString("popis", bary.get(id).getPopis());
dialog.setArguments(args);
dialog.show(getActivity().getFragmentManager(), "MyDialog");
}
};
itemBox.setOnClickListener(handler);
mainLayout.addView(itemBox);
}
return rootView;
}
Can U help me with my problem pls?
Simplest and most straight forward way is to register each fragment on its activity while creating it. Then delegate event made by actionbar button through the activity to appropriate fragment.
Other way is to use some kind event distribution observer pattern or event-bus patter.
I use event-bus implemented by RoboGuice personaly. Other implementations could be found in Guava or Otto libraries.