当我是json解析时,GridView TabView无法正常工作

时间:2015-09-28 05:37:47

标签: android json gridview

它在单个GridView Json对象中正常工作,当我调用tabView时,我的选项卡将显示,但我的Gridview未设置到我的选项卡中。 请任何人帮助我

主要活动

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

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

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

MyAdapter

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new Kids();
    case 1:
        // Games fragment activity
        return new Kids();
    case 2:
        // Movies fragment activity
        return new Kids();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

此活动未显示在我的Tabview中,它在我的单个gridview活动中正常工作,当我在tabview中调用时,它不会显示我的gridview kids.java

公共课孩子扩展片段{

ListAdapter adapter;



private ArrayList<Pojo> gridData;
 GridView grd;
 private ProgressBar mProgressBar;
private String Sam_URL = "http://example.in/rest/jsoMain/document?name=Rekapalli";


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
    System.out.println("Servce Called");
    gridData=new ArrayList<Pojo>();

    grd =(GridView)rootView.findViewById(R.id.gridview);
    Async as=new Async(getActivity(),grd);
    as.execute(Sam_URL);

    mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
   // grd.setBackgroundColor(Color.CYAN);
    grd.setVerticalSpacing(7);
    grd.setHorizontalSpacing(7);


    return super.onCreateView(inflater, container, savedInstanceState);
}







class Async extends AsyncTask<String, Void, Integer>{
    Context context;
    GridView gridView;


    public Async(Context context,GridView gridView) {
        // TODO Auto-generated constructor stub

        this.context=context;
        this.gridView=gridView;
    }



    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub


        Integer result = 0;
        try {
            // Create Apache HttpClient
            //HttpClient httpclient = new DefaultHttpClient();
            URL url = new URL(Sam_URL);
            URLConnection urlConnection = url.openConnection();
            InputStream in = new BufferedInputStream(
                    urlConnection.getInputStream());

            // int statusCode =
            // httpResponse.getStatusLine().getStatusCode();

            // 200 represents HTTP OK
            if (true) {
                String response = streamToString(in);
                parseResult(response);
                result = 1; // Successful
            } else {
                result = 0;       // "Failed
            }
        } catch (Exception e) {

        }

        return result;



    }
    String streamToString(InputStream stream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(stream));
        String line;
        String result = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }

        // Close stream
        if (null != stream) {
            stream.close();
        }
        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        if (result == 1) {

    gridView.setAdapter(new MyAdapter(context,gridData));

    gridView.setVisibility(View.VISIBLE);



    }mProgressBar.setVisibility(View.GONE); 

    }   



    private void parseResult(String result) {
        try {

            Log.d("MainActivity", "JSON Result : " + result);
            JSONArray response = new JSONArray(result);

            for (int i = 0; i < response.length(); i++)

            {

                JSONObject obj = response.getJSONObject(i);

                String Doc_name = obj.getString("documentName");
                Log.d("documentName",Doc_name);

                String Doc_file = obj.getString("documentFile");
            String Doc_content = obj.getString("documentContent");

            String Doc_offer=obj.getString("offer");
            String Doc_address=obj.getString("address");

                //Log.d("documentName","JSON Result : " + result);

                Pojo gd = new Pojo();



                gd.setDocumentName(Doc_name);

                gd.setDocumentFile(Doc_file);
                gd.setOffer(Doc_offer);

                gd.setDocumentContent(Doc_content);
                gd.setAddress(Doc_address);

                gridData.add(gd);


            }


        } catch (JSONException e) {
            e.printStackTrace();
        }


        }
}

}

1 个答案:

答案 0 :(得分:1)

更改自:

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            // Top Rated fragment activity
            return new Kids();
        case 1:
            // Games fragment activity
            return new Kids();
        case 2:
            // Movies fragment activity
            return new Kids();
        }
    return null;
}

要:

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            // Top Rated fragment activity
            return new Kids();
        case 1:
            // Games fragment activity
            return new Games();
        case 2:
            // Movies fragment activity
            return new Movies();
        }

    return null;
}

希望能帮助!!!