このJSON構成があります。サーバーから:
{"t":"AppName",
"tabs":[
{"tbico":"10-medical.png","t":"Twitter","st":"web", "url":"http://www.google.com"},
{"tbico":"101-gameplan.png","t":"GoogleMaps","st":"doc", "file":"abountus.pdf"},
{"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"},
{"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"},
{"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"}
]
}
このレイアウトのタブを設定するために、このクラスを作成しました:
タブビュー:
public class TabView extends FragmentActivity implements ActionBar.TabListener {
//Class Vars
ViewPager dViewPager;
TabviewAdapter dAdapter;
private ArrayList<JsonToDataGen> dSource = new ArrayList<JsonToDataGen>();
//Class Vars:
FileOperation fo = new FileOperation();
PluginManager pm = new PluginManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//** MUST RUN BELOW THREAD BEFOR ANY THING ELSE STARTS **//
new Thread(new Runnable() {
@Override
public void run() {
JsonToDataGenParser parser = new JsonToDataGenParser(fo.load(getApplicationContext(), "config.json"));
dSource = parser.getSource();
Message msg = dHandler.obtainMessage();
dHandler.sendMessage(msg);
}
}).start();
setContentView(R.layout.activity_tab_view);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
dAdapter = new TabviewAdapter(getSupportFragmentManager(), dSource);
// Set up the ViewPager with the sections adapter.
dViewPager = (ViewPager) findViewById(R.id.pager);
dViewPager.setAdapter(dAdapter);
//Get Titles and Fragments
for (int i = 0; i < dAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(dAdapter.getPageTitle(i))
.setTabListener(this));
}
//Swipe-effect
dViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
}//End onCreate
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
dViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}
//Layout view Handler
private Handler dHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
dAdapter.setObjects(dSource);
dAdapter.notifyDataSetChanged();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
}
そしてTabviewアダプター:
public class TabviewAdapter extends FragmentPagerAdapter{
//Class Ref.
PluginManager pm = new PluginManager();
//Local Objects
String clickedObjectRowJson = null;
private ArrayList<JsonToDataGen> dObject;
public TabviewAdapter(FragmentManager fm, ArrayList<JsonToDataGen> dObject) {
super(fm);
this.dObject = dObject;
}
public void setObjects (ArrayList<JsonToDataGen> dObject){
this.dObject = dObject;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
JsonToDataGen item = dObject.get(position);
switch (position) {
case 0:
return item.getT().toUpperCase(l);
case 1:
return item.getT().toUpperCase(l);
case 2:
return item.getT().toUpperCase(l);
case 3:
return item.getT().toUpperCase(l);
case 4:
return item.getT().toUpperCase(l);
}
return null;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
args.putString("clikedObject",clickedObjectRowJson);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
Log.i("TAB ICON ------","TabICON Object Size"+dObject.size());
return (this.dObject.size());
}
}
ここに私の問題があります:JSONを使用して作成した各タブ内に"st" = "picture"
画像をロードしたり"st" = "web"
、指定されたURLでWebサイトをロードしたりするJSONの異なるフラグメントをロードするにはどうすればよいですか?
この 2 つのクラスのどの部分で、オブジェクトから json 値を検索し、それらのフラグメントをビューに配置する必要がありますか、それとも新しいクラスを作成する必要がありますか?
(json 解析の助けは必要ありません)