Reputation: 1567
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.
Custom Adapter Constructor
/**Here in Constructor, context getting null value. App Crashing*/
public HotelCustomAdapter(Context hotelMaster, int textViewResourceId,ArrayList<GetSetOffers> hotelLists) {
super(hotelMaster,textViewResourceId, hotelLists);
hotelList=hotelLists;
context=hotelMaster;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Edited:
Fragment
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.
Fragment class
public class OneFragment extends Fragment implements View.OnClickListener{
View view;
Button ok;
TextView text;
public ListView list;
HotelCustomAdapter hca;
ArrayList<GetSetOffers> 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<GetSetOffers> 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;
}
}
}
postExecute() method
@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 < 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();
}
}
I think Passing the value from AsyncTask postExecute method to Fragment is wrong. Again the fragment context is null.
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.
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
Upvotes: 2
Views: 1471
Reputation: 1567
Finally I merged the two classes. AsyncTask class and Fragment. So, now the context is not null. Inside the fragment I coded the AsyncTask. So now passing the values to Adapter getting the context properly. Now the app working properly. Not crashing.
Thanks @Shoeb Siddique. Our chat conversation give some ideas.
Upvotes: 0
Reputation: 951
//After the value got from postExecute, it was showing null to the context
public void getAdapterView(ArrayList<GetSetOffers> hotelList){
hca=new HotelCustomAdapter(getActivity(),R.layout.hotel_custom_listview,hotelList);
list.setAdapter(hca);
}
Do only this change. No need to change the Context in Adapter. It will work.
Upvotes: 1
Reputation: 2825
Please try this -
//After the value got from postExecute, it was showing null to the context
public void getAdapterView(ArrayList<GetSetOffers> hotelList){
hca=new HotelCustomAdapter(getActivity(),R.layout.hotel_custom_listview,hotelList);
list.setAdapter(hca);
}
And Also do this in Adapter class.
/**Here in Constructor, context getting null value. App Crashing*/
Activity context;
public HotelCustomAdapter(Activity hotelMaster, int textViewResourceId,ArrayList<GetSetOffers> hotelLists) {
super(hotelMaster,textViewResourceId, hotelLists);
hotelList=hotelLists;
this.context=hotelMaster;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Upvotes: 1
Reputation: 30985
Because of issues like that I started doing this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
...
}
Upvotes: 2