I am working in a new Android project.
The first activity is using a slider menu and fragments. On the first fragment there is a list view (PrimaryFragmentDormir.java). After selecting one of the rows, a new activity is launched. This last activity uses three tabs, to show different information about the selected row object. The listview is loaded from remote JSON files.
This is the onItemClick method at PrimaryFragmentDormir.java:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Hotel hotelActual = (Hotel) adapter.getItem(position);
String msg = "Elegiste el hotel " + hotelActual.getNombre();
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), Detalle_Hotel.class);
intent.putExtra("id_hotel", hotelActual.getId_hotel());
intent.putExtra("nombre_hotel", hotelActual.getId_hotel());
intent.putExtra("descripcion_hotel", hotelActual.getId_hotel());
intent.putExtra("latitud_hotel", hotelActual.getId_hotel());
intent.putExtra("longitud_hotel", hotelActual.getId_hotel());
intent.putExtra("direccion_hotel", hotelActual.getId_hotel());
intent.putExtra("web_hotel", hotelActual.getId_hotel());
intent.putExtra("tel_hotel", hotelActual.getId_hotel());
intent.putExtra("tel_reservas", hotelActual.getId_hotel());
intent.putExtra("foto_hotel", hotelActual.getId_hotel());
intent.putExtra("calificacion_hotel", hotelActual.getId_hotel());
intent.putExtra("num_estrellas", hotelActual.getId_hotel());
intent.putExtra("zona_hotel", hotelActual.getId_hotel());
intent.putExtra("facebook_hotel", hotelActual.getFacebook());
intent.putExtra("twitter_hotel", hotelActual.getTwitter());
startActivity(intent);
}
The Toast is shown and the activity Detalle_Hotel is shown also.
Detalle_Hotel has three tabs. What I need is to get the values from hotelActual in the three tabs, in order to work with them separately.
This is Detalle_Hotel activity:
public class Detalle_Hotel extends AppCompatActivity {
// Declaring Your View and Variables
private String nombre_hotel, foto_hotel, descripcion_hotel,direccion_hotel,web_hotel,tel_hotel,tel_reservas,zona_hotel,facebook_hotel,twitter_hotel;
private int num_estrellas_hotel, id_hotel;
private double calificacion_hotel,latitud_hotel,longitud_hotel;
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Info","Mapa","Opinión"};
int Numboftabs =3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle__hotel);
nombre_hotel = getIntent().getStringExtra("nombre_hotel");
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.rojomodesto);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
}
Here I received the value from nombre_hotel (as test for the other values), and now how can I pass it to the tabs?
Here is tab1 code:
public class Tab1 extends Fragment {
private TextView hotel_nombre;
private String nombre_hotel;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
hotel_nombre = (TextView) getView().findViewById(R.id.nombre_hotel);
hotel_nombre.setText(getActivity().nombre_hotel));
}
}
The line hotel_nombre.setText(getActivity().nombre_hotel));
shows a warning at the second nombre_hotel: "Cannot resolve symbol 'nombre_hotel'.
Any help is welcome.
EDIT:
ViewPageAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
}
if (position == 1) // if the position is 0 we are returning the First tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
if (position == 2) // if the position is 0 we are returning the First tab
{
Tab3 tab3 = new Tab3();
return tab3;
}
return null;
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
In your adapter you need to initialize it properly now (with string as argument).
public class Tab1 extends Fragment {
private static final String HOTEL = "hotel";
private TextView hotel_nombre;
private String nombre_hotel;
public static Tab1 newInstance(String s) {
Tab1 result = new Tab1();
Bundle bundle = new Bundle();
bundle.putString(HOTEL, s);
result.setArguments(bundle);
return result;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
nombre_hotel = bundle.getString(HOTEL);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
hotel_nombre = (TextView) getView().findViewById(R.id.nombre_hotel);
hotel_nombre.setText(nombre_hotel);
}
}
EDIT:
Change your in your Detalle_Hotel
Activity, adapter to:
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,nombre_hotel);
And then in adapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private String hotelNumbre;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb, String hotelNum) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
this.hotelNumbre = hotelNum;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if (position == 0) // if the position is 0 we are returning the First tab
{
return Tab1.newInstance(hotelNumbre);
}
if (position == 1) // if the position is 0 we are returning the First tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
if (position == 2) // if the position is 0 we are returning the First tab
{
Tab3 tab3 = new Tab3();
return tab3;
}
return null;
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}