title: Custom Adapter getting Null Context from Fragment
body: <p>I am working with Tab Layout. I am getting the data from live DB to show the information in listview. For that I am using CustomAdapter. The value which in the arraylist is getting properly. Once the Arraylist data you passed to CustomAdapter then the Context getting null exception. How to pass the context of Fragment to Custom Adapter.</p>
  
  <p>Custom Adapter Constructor</p>
  
  <pre><code>/**Here in Constructor, context getting null value. App Crashing*/
  public HotelCustomAdapter(Context hotelMaster, int textViewResourceId,ArrayList&lt;GetSetOffers&gt; hotelLists) {
      super(hotelMaster,textViewResourceId, hotelLists);
      hotelList=hotelLists;
      context=hotelMaster;
      inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  </code></pre>
  
  <p><strong>Edited:</strong></p>
  
  <p>Fragment</p>
  
  <p>From the fragment I am calling AsyncTask class to get the data from cloud. From postExecute I am passing the Arraylist to method of fragment. At that point context is null. How to fix this.</p>
  
  <p>Fragment class</p>
  
  <pre><code>public class OneFragment extends Fragment implements View.OnClickListener{
  View view;
  Button ok;
  TextView text;
  public ListView list;
  HotelCustomAdapter hca;
  ArrayList&lt;GetSetOffers&gt; temp;
  Context context;
  public OneFragment() {
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      ScrollView scroller = new ScrollView(getActivity());
  
      view = inflater.inflate(R.layout.fragment_one, container, false);
      ok = (Button) view.findViewById(R.id.ok);
      text = (TextView)view.findViewById(R.id.text);
      list = (ListView) view.findViewById(R.id.list);
      ok.setOnClickListener(this);
      context=view.getContext(); // Here the context is assigned
      new AsyncHotel(getActivity()).execute("19");
      return view;
  }
  
  //After the value got from postExecute, it was showing null to the context
  public void getAdapterView(ArrayList&lt;GetSetOffers&gt; hotelList){
      //temp=hotelList;
      hca=new HotelCustomAdapter(context,R.layout.hotel_custom_listview,hotelList);
      list.setAdapter(hca);
  }
  
  @Override
  public void onClick(View v) {
      switch (v.getId()) {
          case R.id.ok:
              text.setText("Y You Clicked.?");
      //hca=new HotelCustomAdapter(getActivity(),R.layout.hotel_custom_listview,temp);
      //list.setAdapter(hca);     // tried this
              break;
      }
  }
  
  }
  </code></pre>
  
  <p>postExecute() method</p>
  
  <pre><code>@Override
  protected void onPostExecute(String result) {
      try {
          root = new JSONObject(result);
          validation = root.getInt("response");
          JSONArray data = root.getJSONArray("data");
  
          if (validation == 1) {
              for (int i = 0; i &lt; data.length(); i++) {
                  hgs = new GetSetOffers(); 
                  hgs.setOfferTitle(data.getJSONObject(i).getString("hotel_name"));
                  hgs.setOfferDescrip(data.getJSONObject(i).getString("city"));
                  hgs.setOfferToDt(data.getJSONObject(i).getString("hotel_name"));
  
                  hotelList.add(hgs);
              }
              OneFragment one=new OneFragment(); // I think here I am doing wrong
              one.getAdapterView(hotelList);
          }
    } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I think Passing the value from AsyncTask postExecute method to Fragment is wrong. Again the fragment context is null.</p>
  
  <p>The context is globally declared. Before calling the AsyncTask class the context variable is assigned with the Activity. That Fragment context has value now. AsyncTask class finish its task and then the list is passed to the fragment, by creating new object to that fragment in postExecute() method and before passing that list to Custom Array Adapter and  the context is null. Please see the image.</p>
  
  <p><a href="https://i.stack.imgur.com/UDcBE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UDcBE.png" alt="enter image description here"></a></p>
  
  <p>Again I tried to assign that received list to global variable in Fragment. Then after the button click I tried to pass the list to Custom Array Adapter. Now context has value and list changed changed to empty even that list is assigned to a global variable</p>