2

我正在使用選項卡布局。我從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()方法中將該列表傳遞給片段,並在將該列表傳遞給自定義數組適配器並且該上下文爲空之前,通過在該片段中創建新對象。請參閱圖片。

enter image description here

我又一次試圖分配,在片段收到列表中的全局變量。然後點擊按鈕後,我嘗試將列表傳遞給自定義數組適配器。現在,上下文值和列表變了變空,即使該列表被分配到一個全局變量

+0

創建一個全局上下文變量,在您的onCreateView()中爲其分配view.getContext(),然後在其他地方使用它 –

+0

謝謝@Nick Isaacs,對不起,對於最近的回覆。內部onCreateView()上下文變量變爲空。上下文= view.getContext(); –

回答

0

最後我合併了兩個類。 AsyncTask類和片段。所以,現在上下文不爲空。在片段中我編碼了AsyncTask。所以現在將值傳遞給Adapter以正確地獲取上下文。現在應用程序正常工作。沒有崩潰。

謝謝@Shoeb Siddique。我們的聊天對話提供了一些想法

2

由於這樣的問題,我開始做這個:

 @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); 
      ... 
     } 
+0

謝謝@kris larson。在自定義適配器類構造函數內部爲上下文獲取null。然後如何調用getView()方法。 –

+0

編輯該問題請檢查一次。將列表傳遞給Fragment類時,片段上下文爲空。 –

1

請試試這個 -

//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); 
} 

並且還在Adapter類中執行此操作。

/**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); 
} 
+0

謝謝。請參閱上面的圖片。我試過你的代碼。我的代碼在傳遞到自定義數組適配器之前也會在上下文中顯示相同的空值 –

+0

是的,您的Fragment上下文顯示爲空,但是您可以檢查Adapter中的上下文嗎? –

+0

是一樣的。我傳遞null,在適配器也null –

1
//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); 
} 

只做這種變化。無需更改適配器中的上下文。它會工作。

+0

謝謝。請參閱上面的圖片。我試過你的代碼。上下文相同 –