我正在使用選項卡布局。我從live DB獲取數據以在listview中顯示信息。爲此,我使用CustomAdapter。數組列表中的值正確得到。一旦你傳遞給CustomAdapter的Arraylist數據,那麼上下文獲得null異常。如何將Fragment的上下文傳遞給自定義適配器。自定義適配器從片段獲取空上下文
定義適配器構造
/**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);
}
編輯:
片段
從我打電話的AsyncTask類從雲中獲取數據的片段。從postExecute中,我將Arraylist傳遞給fragment的方法。此時上下文無效。如何解決這個問題。
片段類
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()方法
@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();
}
}
我想從的AsyncTask postExecute方法傳遞的值以片段是錯誤的。片段上下文再次爲空。
上下文是全局聲明的。在調用AsyncTask類之前,上下文變量被分配給活動。該片段環境現在具有價值。 AsyncTask類完成其任務,然後通過在postExecute()方法中將該列表傳遞給片段,並在將該列表傳遞給自定義數組適配器並且該上下文爲空之前,通過在該片段中創建新對象。請參閱圖片。
我又一次試圖分配,在片段收到列表中的全局變量。然後點擊按鈕後,我嘗試將列表傳遞給自定義數組適配器。現在,上下文值和列表變了變空,即使該列表被分配到一個全局變量
創建一個全局上下文變量,在您的onCreateView()中爲其分配view.getContext(),然後在其他地方使用它 –
謝謝@Nick Isaacs,對不起,對於最近的回覆。內部onCreateView()上下文變量變爲空。上下文= view.getContext(); –