gpt4 book ai didi

android - 通过传递的 JSON 在 Android 中生成/创建动态选项卡

转载 作者:行者123 更新时间:2023-11-29 16:46:39 25 4
gpt4 key购买 nike

我正在尝试通过 Volley 从加载的 JSON 文件动态创建选项卡(然后在每个选项卡的 fragment 中添加另一个动态创建的内容),但没有成功。

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

String myData = "";

public static String test;

//Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
//Title List
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
loaddata();
//create multiple titles, but use OneFragment() for every new tab
for(int i = 0; i < mFragmentTitleList.size(); i++) {
mFragmentList.add(new OneFragment());
}
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
}

//Loading the JSON data, which will be used to create dynamically content such as tabs and info for the Fragments
private void loaddata() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://ipfs.io/ipfs/QmQ69ScwoqSsFAdGV4oUKNaDMzSDuY9zcdRoFYkHPUZM7E";
String line = "";

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
myData = response;
//Log.d("response", myData);

JSONObject object = null;
try {
object = new JSONObject(myData);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject menuitems = null;
try {
menuitems = object.getJSONObject("menuitems");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i<menuitems.length(); i++) {
try {
mFragmentTitleList.add(menuitems.names().getString(i));
//mFragmentTitleList gives output [Alcohol Drinks, Soups, Salads]
Log.d("test", String.valueOf(mFragmentTitleList));
} catch (JSONException e) {
e.printStackTrace();
}
//Log.d(TAG, "Key = " + menuitems.names().getString(i) + " value = " + menuitems.get(menuitems.names().getString(i)));
}
/*try {
Log.d("testagain", "\"" +categories.getString(2)+ "\"");
} catch (JSONException e) {
e.printStackTrace();
}
*/

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, "That didn't work!", duration);
}
});
queue.add(stringRequest);
}

private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);
viewPager.setAdapter(adapter);
}


//ViewPagerAdapter settings
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}

}

最后我应该有 3 个标签,分别是 Alcohol Drinks、Soups、Salads

目前应用程序刚刚启动,没有生成任何选项卡。谢谢你帮助我! :)

最佳答案

您必须在 loaddata -> onResponse 填充“mFragmentList”列表后创建选项卡。

编辑粘贴这段代码

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

String myData = "";

public static String test;

//Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
//Title List
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
loaddata();

tabLayout = (TabLayout) findViewById(R.id.tabs);
}

//Loading the JSON data, which will be used to create dynamically content
such as tabs and info for the Fragments
private void loaddata() {
RequestQueue queue = Volley.newRequestQueue(this);
String url =
"https://ipfs.io/ipfs/QmQ69ScwoqSsFAdGV4oUKNaDMzSDuY9zcdRoFYkHPUZM7E";
String line = "";

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
myData = response;
//Log.d("response", myData);

JSONObject object = null;
try {
object = new JSONObject(myData);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject menuitems = null;
try {
menuitems = object.getJSONObject("menuitems");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i<menuitems.length(); i++) {
try {
mFragmentTitleList.add(menuitems.names().getString(i));
//mFragmentTitleList gives output [Alcohol Drinks, Soups, Salads]
Log.d("test", String.valueOf(mFragmentTitleList));
} catch (JSONException e) {
e.printStackTrace();
}
//Log.d(TAG, "Key = " + menuitems.names().getString(i) + " value = " + menuitems.get(menuitems.names().getString(i)));
}
//create multiple titles, but use OneFragment() for every new tab
for(int i = 0; i < mFragmentTitleList.size(); i++) {
mFragmentList.add(new OneFragment());
}
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
/*try {
Log.d("testagain", "\"" +categories.getString(2)+ "\"");
} catch (JSONException e) {
e.printStackTrace();
}
*/

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, "That didn't work!", duration);
}
});
queue.add(stringRequest);
}

private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);
viewPager.setAdapter(adapter);
}


//ViewPagerAdapter settings
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}

}
}

关于android - 通过传递的 JSON 在 Android 中生成/创建动态选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47697781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com