自定义适配器从片段获取空上下文

Custom Adapter getting Null Context from Fragment

我正在使用选项卡布局。我正在从实时数据库获取数据以在列表视图中显示信息。为此,我正在使用 CustomAdapter。 arraylist 中的值正在正确获取。一旦您将 Arraylist 数据传递给 CustomAdapter,然后 Context 获取空异常。如何将 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 class 从云端获取数据。从 postExecute 我将 Arraylist 传递给片段方法。此时上下文为空。如何解决这个问题。

片段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() 方法

@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 方法传递给 Fragment 是错误的。同样,片段上下文为空。

上下文已全局声明。在调用 AsyncTask class 之前,上下文变量被分配了 Activity。该 Fragment 上下文现在有价值。 AsyncTask class 完成其任务,然后通过在 postExecute() 方法中为该片段创建新对象并在将该列表传递给自定义数组适配器之前将列表传递给片段,并且上下文为空。请看图

我再次尝试将接收到的列表分配给 Fragment 中的全局变量。然后在单击按钮后,我尝试将列表传递给自定义阵列适配器。现在上下文有值并且列表更改为空,即使该列表已分配给全局变量

因为这样的问题,我开始这样做:

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

请试试这个 -

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

并且也在适配器中执行此操作 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); 
} 
//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);
} 

只做这个改变。无需更改适配器中的上下文。它会起作用。

最后我合并了两个 classes。 AsyncTask class 和片段。所以,现在上下文不为空。在片段中,我编写了 AsyncTask。所以现在将值传递给适配器以正确获取上下文。现在该应用程序正常运行。不崩溃。

谢谢@Shoeb Siddique。我们的聊天对话提供了一些想法。