getLayoutInflater()和Fragment

时间:2016-05-09 20:37:28

标签: android json android-fragments

我收到错误,getLayountInflator(Bundle)不能在片段内使用。

我有这个JSONAdapter.java

public class JSONAdapter extends BaseAdapter{

    Context mContext;
    LayoutInflater mInflater;
    JSONArray mJsonArray;

    public JSONAdapter(Context context, LayoutInflater inflater) {
        mContext = context;
        mInflater = inflater;
        mJsonArray = new JSONArray();
    }

    @Override
    public int getCount() {
        return mJsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        return mJsonArray.optJSONObject(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {


            convertView = mInflater.inflate(R.layout.article_list, parent, false);


            holder = new ViewHolder();
            holder.thumbnailImageView = (ImageView) convertView.findViewById(R.id.img_thumbnail);
            holder.titleTextView = (TextView) convertView.findViewById(R.id.text_title);
            holder.categoryTextView = (TextView) convertView.findViewById(R.id.text_category);


            convertView.setTag(holder);
        } else {


            holder = (ViewHolder) convertView.getTag();
        }

        JSONObject jsonObject = (JSONObject) getItem(position);




            String imageURL = jsonObject.optString("some_image_url");

           Picasso.with(mContext).load(imageURL).placeholder(R.mipmap.impakt_icon).into(holder.thumbnailImageView);



        String articleTitle = "";
        String categoryName = "";


            categoryName = jsonObject.optJSONObject("featured_categories_name").optString("cat_name");
   }



        holder.titleTextView.setText(articleTitle);
        holder.categoryTextView.setText(categoryName);

        return convertView;
    }


    private static class ViewHolder {
       public ImageView thumbnailImageView;
        public TextView titleTextView;
        public TextView dateTextView;
        public TextView categoryTextView;
    }


    public void updateData(JSONArray jsonArray) {

        mJsonArray = jsonArray;
    }

}

和这个布局适配器SlidingTabsBasicFragment.java

public class SlidingTabsBasicFragment extends ListFragment {

    static final String LOG_TAG = "SlidingTabsBasicFragment";

    private SlidingTabLayout mSlidingTabLayout;


    private ViewPager mViewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new SamplePagerAdapter());

        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(mViewPager);

    }


    class SamplePagerAdapter extends PagerAdapter implements View.OnClickListener {

        ListView lstTest;
        JSONAdapter mJSONAdapter;

        String tabs[]={"Item 1","Item 2","Item 3","Item 4","Item 5","Item 1","Item 1","Item 1"};
        /**
         * @return the number of pages to display
         */
        @Override
        public int getCount() {
            return tabs.length;
        }

        /**
         * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
         * same object as the {@link View} added to the {@link ViewPager}.
         */
        @Override
        public boolean isViewFromObject(View view, Object o) {
            return o == view;
        }


       @Override
        public CharSequence getPageTitle(int position) {
           return tabs[position];
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View view=null;
            if(position%2==0){
                view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,container, false);
                lstTest = (ListView) view.findViewById(R.id.home_list);
                Button but = (Button) view.findViewById(R.id.button1);
              mJSONAdapter = new JSONAdapter(getActivity(),getLayoutInflater());
                // Set the ListView to use the ArrayAdapter
                lstTest.setAdapter(mJSONAdapter);
                but.setOnClickListener(this);


            }else{
                view = getActivity().getLayoutInflater().inflate(R.layout.economics_pager_item,container, false);
            }
            // Add the newly created View to the ViewPager
            container.addView(view);

            // Retrieve a TextView from the inflated View, and update it's text
            TextView title = (TextView) view.findViewById(R.id.item_title);
            title.setText(String.valueOf(position + 1));


            return view;
        }




        View view=null;
        if(position%2==0){
            view = activity.getLayoutInflater().inflate(R.layout.home_page_item,container, false);
        }else{
            view = activity.getLayoutInflater().inflate(R.layout.ekonomia_page_item,container, false);
        }

        container.addView(view);

        // Retrieve a TextView from the inflated View, and update it's text
        TextView title = (TextView) view.findViewById(R.id.item_title);
        title.setText(tabs[position]);

        // Return the View
        return view;
    }



        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);


        @Override
        public void onClick(View v) {
            queryImp();

        }

        private void queryImpakt() {

            // Create a client to perform networking
            AsyncHttpClient client = new AsyncHttpClient();

            // Have the client get a JSONArray of data
            // and define how to respond
            client.get("http://xxxxxxx",


                    new JsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(JSONArray jsonArray) {
                            // Display a "Toast" message
                            // to announce your success
                           // Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();


                            // update the data in your custom method.
                            mJSONAdapter.updateData(jsonArray);
                            mJSONAdapter.notifyDataSetChanged();

                        }

                        @Override
                        public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                            // Display a "Toast" message
                            // to announce the failure
                           // Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();

                            // Log error message
                            // to help solve any problems

                        }
                    });

        }

    }
}

让我错误的一行是:

mJSONAdapter = new JSONAdapter(getActivity(),getLayoutInflater());

on JSONAdapter.java

我该如何解决,我需要做什么

1 个答案:

答案 0 :(得分:1)

您可以使用getActivity().getLayoutInflater()来检索活动的LayoutInflater对象。

更改

mJSONAdapter = new JSONAdapter(getActivity(), getLayoutInflater());

mJSONAdapter = new JSONAdapter(getActivity(), getActivity().getLayoutInflater());