0

我有这个 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"在我使用 json 创建的每个选项卡中加载具有给定 url 的网站?

在这两个类的哪一部分中,我应该从对象中查找 json 值并将这些片段放在 View 上,还是应该创建新类?

(我不需要任何 json 解析的帮助)

4

1 回答 1

0

在 TabviewAdapter 类中,

public Fragment getItem(int position) 

该方法将返回每个页面的片段,带有位置参数,您可以访问json中的值。

于 2013-07-17T02:35:07.000 回答