首先,对不起我的英语。
我创建了一个带有3个标签的活动,这些标签由FragmentPagerAdapter处理。但是我想在选项卡中显示的信息在activity.class中。我是一个初学者,我看不到如何将数据从activity.class传输到FragmentPagerAdapter
这是为了“了解更多”,当我单击1个consommer时,我进入带有3个选项卡的Activity,在on上创建一个对返回JSONObject的Web API的url请求,我解析此json并获取其他变量。这些变量我不想放在putStrings中的FragmentPagerAdapter上
public class ViewPagerAdapter extends FragmentPagerAdapter {
private String[] libelle = { "Coordonnées", "Contacts", "TI" };
ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Coordonne_client coordonneClient = new Coordonne_client();
Bundle bundle = new Bundle();
bundle.putString("nomCli", "");
coordonneClient.setArguments(bundle);
return coordonneClient;
case 1:
Contacts_client contacts_client = new Contacts_client();
Bundle bundle2 = new Bundle();
bundle2.putString("message", "Fragment: Liste de contact");
bundle2.putString("test", "message test");
contacts_client.setArguments(bundle2);
return contacts_client;
case 2:
Tickets_incidents tickets_incidents = new Tickets_incidents();
Bundle bundle3 = new Bundle();
bundle3.putString("message", "Fragments = Liste des TI du client");
tickets_incidents.setArguments(bundle3);
return tickets_incidents;
default:
return null;
}
}
@Override
public int getCount(){
return 3;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return libelle[position];
}
}
和活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_client);
viewPager=findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout=findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
Bundle extras = getIntent().getExtras();
String leclient = (String) extras.get("leClient");
queue = Volley.newRequestQueue(InfoClient.this);
url = "http://host/select/infoClient/"+leclient;
stringRequest = new StringRequest(GET, url, InfoClient.this, InfoClient.this);
queue.add(stringRequest);
}
@Override
public void onResponse(String response) {
try{
JSONObject client = new JSONObject(response);
String nomClient = client.get("NOM").toString();
String adresseClient = client.get("ADRESSE").toString();
String codePostalClient = client.get("CODEP").toString();
String villeClient = client.get("VILLE").toString();
String telClient = client.get("TEL").toString();
}catch (JSONException e) {
e.printStackTrace();
}
}
感谢您的帮助,希望您理解。
最佳答案
将您的约会放入捆绑包中,然后使用
fragment.setArguments(捆绑包含数据)
关于java - FragmentPagerAdapter面向初学者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58526497/