我有一个JSON,可以找回昨天,今天和明天的日期,我有一个带有三个标签的滑动菜单,我希望他的三个标签的标题是昨天的第一个日期并继续...
我已经解析了JSON并检索了数据,现在我需要找到一种方法在setupviewpager中“导出”这些数据并设置标题 也许我需要修改GetPageTitle?
感谢您的帮助
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private ProgressDialog pDialog;
String Yesterday = "";
String Today = "";
String Tomorrow = "";
//url per il recupero del JSON
private static String url = "there is an url here";
private ListView TodayMenu;
ArrayList<String> DATEJSON = new ArrayList<String>();
ArrayList<HashMap<String, String>> DATA;
ViewPager viewer;
TabLayout tabsgtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbargtv = (Toolbar) findViewById(R.id.toolbargtv);
setSupportActionBar(toolbargtv);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewer = (ViewPager) findViewById(R.id.viewer);
//setupViewPager(viewer);
tabsgtv = (TabLayout) findViewById(R.id.tabsgtv);
tabsgtv.setupWithViewPager(viewer);
TodayMenu = (ListView) findViewById(R.id.todayMenu);
new GetData().execute();
setupViewPager(viewer);
}
private class GetData extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
super.onPreExecute();
//Mostro avviso
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Attendi..");
pDialog.setCancelable(false);
pDialog.show();
}
public class Date{
private String yesterday;
private String today;
private String tomorrow;
}
@Override
protected Void doInBackground(Void... arg0){
HttpHandler sh = new HttpHandler();
//faccio una richiesta al server e attendo risposta
String jsonStr = sh.makeServiceCall(url);
Log.e("RAW-JSON: ","Retrieve RAW-Json is "+jsonStr);
if (jsonStr != null) {
try {
Log.e("Coming here??", "Check 1");
JSONObject jsonObj = new JSONObject(jsonStr);
String DATESTRING = jsonObj.getString("date");
JSONObject DATE = new JSONObject(DATESTRING); //Log.d("STAMPA","DATESTRING"+DATESTRING);
//Log.d("PER FAVORE","IERI ERA "+Ieri);
//ESTRAGGO E INSERISCO DOVE MI SERVE
Yesterday = DATE.getString("ieri");
Today = DATE.getString("oggi");
Tomorrow = DATE.getString("domani");
Log.e("Coming here??", "Check 2 values are -" + Yesterday + Today + Tomorrow);
} catch (JSONException e) {
Log.e("ERRORE", "PANICO NERO");
runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(getApplicationContext(),"Errore codifica dati",Toast.LENGTH_LONG).show();
}
});
}
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Errore recupero dati",Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
//tolgo il caricamento di pDialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new YesterdayFragment(),"");
adapter.addFragment(new TodayFragment(),"");
adapter.addFragment(new TomorrowFragment(),"");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {//TODO FORSE QUI
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
答案 0 :(得分:1)
首先将这些值保存在数组列表中。
Yesterday = DATE.getString("ieri");
Today = DATE.getString("oggi");
Tomorrow = DATE.getString("domani");
//for e.g. arrayList.add(Yesterday); & same for Today and tomorrow
Log.e("Coming here??", "Check 2 values are -" + Yesterday + Today + Tomorrow);
保存后只需使用for循环并获取制表符的位置并设置值。
TabLayout.Tab tab = tabLayout.getTabAt(position);
tab.setText("title");