Passing values from activity to tabs

mvasco

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;
    }
}
Damian Kozlak

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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android - Passing View from Activity to AsyncTask class

From Dev

Passing Picture from one activity to another activity

From Dev

Passing String From Activity to Fragment

From Dev

Calling a fragment method from an activity android tabs

From Dev

Passing data from Activity to Fragments

From Dev

Passing data from BroadCastRecevier to activity

From Dev

Passing Objects from Activity to ViewPager and then to Fragment

From Dev

Passing Data from Dialog box to activity

From Dev

Passing String values from custom dialog to another activity

From Dev

Passing data from activity to service through interface

From Dev

passing values from onClick

From Dev

Passing strings from an Activity to a DialogFragment

From Dev

Passing values through bundle and to get it on another activity

From Dev

Passing Object from Fragment to Activity

From Dev

Passing Parameters from Activity to Another Activity Fragements

From Dev

Passing location data from fragment to activity in Android

From Dev

passing value from activity to adapter

From Dev

passing string from an activity to a fragment

From Dev

Passing Values from one activity to another, using Object and getting a resource not found error.

From Dev

passing values from radiobutton?

From Dev

Getting null values when passing data from one activity to another in android

From Dev

Passing values from an Activity into a ListView in another activity and saving these values

From Dev

Passing variable from Activity to Adapter

From Dev

Passing String values from custom dialog to another activity

From Dev

Adding integer values retrieved from EditText by saving the Bundle and passing it to other activity in Android

From Dev

Passing Parameters from Activity to Another Activity Fragements

From Dev

Passing data from second activity to first activity

From Dev

Class to Activity passing ArrayList values

From Dev

Start a tab fragments from an activity with the tabs layout

Related Related

  1. 1

    Android - Passing View from Activity to AsyncTask class

  2. 2

    Passing Picture from one activity to another activity

  3. 3

    Passing String From Activity to Fragment

  4. 4

    Calling a fragment method from an activity android tabs

  5. 5

    Passing data from Activity to Fragments

  6. 6

    Passing data from BroadCastRecevier to activity

  7. 7

    Passing Objects from Activity to ViewPager and then to Fragment

  8. 8

    Passing Data from Dialog box to activity

  9. 9

    Passing String values from custom dialog to another activity

  10. 10

    Passing data from activity to service through interface

  11. 11

    passing values from onClick

  12. 12

    Passing strings from an Activity to a DialogFragment

  13. 13

    Passing values through bundle and to get it on another activity

  14. 14

    Passing Object from Fragment to Activity

  15. 15

    Passing Parameters from Activity to Another Activity Fragements

  16. 16

    Passing location data from fragment to activity in Android

  17. 17

    passing value from activity to adapter

  18. 18

    passing string from an activity to a fragment

  19. 19

    Passing Values from one activity to another, using Object and getting a resource not found error.

  20. 20

    passing values from radiobutton?

  21. 21

    Getting null values when passing data from one activity to another in android

  22. 22

    Passing values from an Activity into a ListView in another activity and saving these values

  23. 23

    Passing variable from Activity to Adapter

  24. 24

    Passing String values from custom dialog to another activity

  25. 25

    Adding integer values retrieved from EditText by saving the Bundle and passing it to other activity in Android

  26. 26

    Passing Parameters from Activity to Another Activity Fragements

  27. 27

    Passing data from second activity to first activity

  28. 28

    Class to Activity passing ArrayList values

  29. 29

    Start a tab fragments from an activity with the tabs layout

HotTag

Archive