将值从 activity 传递到选项卡

Passing values from activity to tabs

我在一个新的 Android 项目中工作。

第一个 activity 使用滑块菜单和片段。在第一个片段上有一个列表视图 (PrimaryFragmentDormir.java)。选择其中一行后,将启动一个新的 activity。最后一个 activity 使用三个选项卡来显示有关所选行对象的不同信息。 列表视图是从远程 JSON 文件加载的。

这是 PrimaryFragmentDormir.java:

处的 onItemClick 方法
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);




    }

显示 Toast 并显示 activity Detalle_Hotel。

Detalle_Hotel 有三个选项卡。 我需要的是从三个选项卡中的 hotelActual 获取值,以便分别使用它们。

这是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);



    }



}

这里我收到了来自 nombre_hotel 的值(作为对其他值的测试),现在如何将它传递给选项卡?

这是tab1代码:

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

hotel_nombre.setText(getActivity().nombre_hotel)); 行在第二个 nombre_hotel 处显示警告:“无法解析符号 'nombre_hotel'。

欢迎任何帮助。

编辑:

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

在您的适配器中,您现在需要正确初始化它(使用字符串作为参数)。

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

}

编辑:

将您的 Detalle_Hotel Activity 适配器更改为:

adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,nombre_hotel);

然后在适配器中:

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

您需要将 nombre_hotel 字段设置为 public 而不是私有字段,然后使用:

hotel_nombre.setText((Detalle_Hotel)getActivity().nombre_hotel));
 val intent = Intent(activity, VoiceCommandServiceActivity::class.java)
        intent.putExtra(SELECT_SERVICES, mServiceName as Serializable)
        startActivity(activity, intent)

intent?.let {
            it.extras?.let { extras ->
                extras?.let { bundle ->
                    if (bundle.containsKey(SELECT_SERVICES)) {
                        val service = extras.getSerializable(SELECT_SERVICES) as MutableList<AppServiceModel>
                    }
                }
            }
        }