2016-05-09 19 views
0

フラグメントの内部でgetLayountInflator(Bundle)を使用できないというエラーが表示されます。getLayoutInflater()およびフラグメント

私が持っているこの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 

         } 
        }); 

     } 

    } 
} 

行は次のようになります。JSONAdapter上

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

.java

どうすればいいですか?

+0

可能な重複を有する(http://stackoverflow.com/questions/26368178/getlayoutinflater-in-fragment) –

+0

ジャスト'LayoutInflater.from(listView.getContext())'を使用してください。 'listView'はアダプタを接続する場所です。 –

答えて

1

getActivity().getLayoutInflater()を使用して、アクティビティのLayoutInflaterオブジェクトを取得できます。

変更

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

[断片でgetLayoutInflater()]の

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

うわー...私はそれを信じていません。..すべてを試しましたが、これです。ありがとうございました – ddnice

関連する問題