2017-08-16 128 views
-1

我正面臨着來自服務器的JSON數據中的問題保存在ArrayList在自定義適配器中調用ArrayList時返回null。自定義適配器用於在android中的微調器中列出數據。Arraylist在異步方法中返回null?

public class PhotoCommnFragment extends android.support.v4.app.Fragment { 
EditText rechargeMobileNumber,rechargeAmount; 
Spinner selectMenu; 
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon}; 
List<SpinnerMenu> selectedNetwork = new ArrayList<>(); 
public PhotoCommnFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false); 
    mobileRecahrgeHistory(); 
    rechargeMobileNumber = (EditText)rootView.findViewById(R.id.recharge_mobile_number); 
    rechargeAmount = (EditText)rootView.findViewById(R.id.recharge_amount); 
    selectMenu = (Spinner)rootView.findViewById(R.id.selectNetwork); 
    settingSpinnerDropDown(); 
    return rootView; 
} 

public void mobileRecahrgeHistory(){ 
    Ion.with(this) 
      .load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1") 
      .asJsonObject().withResponse() 
      .setCallback(new FutureCallback<Response<JsonObject>>() { 
       @Override 
       public void onCompleted(Exception e, Response<JsonObject> result) { 
        JSONObject json = null; 

        try { 
         json = new JSONObject(result.getResult().toString()); 
        } catch (JSONException e1) { 
         e1.printStackTrace(); 
        } 
        // Create the root JSONObject from the JSON string. 
        JSONObject jsonRootObject = null; 
        jsonRootObject = json.optJSONObject("DS"); 

        //Get the instance of JSONArray that contains JSONObjects 
        JSONArray jsonArray = jsonRootObject.optJSONArray("LST"); 

        //Iterate the jsonArray and print the info of JSONObjects 
        for(int i=0; i < jsonArray.length(); i++){ 
         JSONObject jsonObject = null; 
         try { 
          jsonObject = jsonArray.getJSONObject(i); 
         } catch (JSONException e1) { 
          e1.printStackTrace(); 
         } 
         String iph = null; 

         String oid = jsonObject.optString("OID").toString(); 
         String ocd = jsonObject.optString("OCD").toString(); 
         String opd = jsonObject.optString("OPE").toString(); 
         String mil = jsonObject.optString("MIL").toString(); 
         String mxl = jsonObject.optString("MXL").toString(); 
         try { 
          iph = jsonObject.getString("IPH").toString(); 
         } catch (JSONException e1) { 
          e1.printStackTrace(); 
         } 

         String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph; 
         Bitmap mIcon11 = null; 
         try { 
          InputStream in = new java.net.URL(urldisplay).openStream(); 
          mIcon11 = BitmapFactory.decodeStream(in); 
         } catch (Exception e3) { 
          e3.printStackTrace(); 
         } 
         SpinnerMenu spinnerData = new SpinnerMenu(); 
         spinnerData.setOid(oid); 
         spinnerData.setOcd(ocd); 
         spinnerData.setOpd(opd); 
         spinnerData.setMil(mil); 
         spinnerData.setMix(mxl); 
         spinnerData.setImage(mIcon11); 

         selectedNetwork.add(spinnerData); 

        } 

       } 

      }); 
} 


public void settingSpinnerDropDown(){ 
    Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork); 
    selectMenu.setAdapter(customAdapter); 
} 
在aove方法從網絡中的數據被保存在ArrayList中,但是當ArrayList中被稱爲 Fragment_DTH_Main_Spinner_Adapter` ArrayList中被返回null

。我通過調試來檢查數據是否保存在arraylist中。 我在stackoverflow refferred,你應該在你的方法中添加

Adapter.notifyDataSetChanged()。我不知道在哪裏添加這個方法。在我的方法中調用自定義適配器之前調用的異步方法。那麼使用這種方法有什麼用處。請幫幫我。

下面的代碼是Fragment_DTH_Main_Spinner_Adapter適配器類

public class Fragment_DTH_Main_Spinner_Adapter extends ArrayAdapter<SpinnerMenu> { 
     Context context; 
    int flags[]; 
    List<SpinnerMenu> countryNames; 
    LayoutInflater inflter; 

    public Fragment_DTH_Main_Spinner_Adapter(FragmentActivity activity, int resouceId, int textviewId, List<SpinnerMenu> data) { 


    //  super(activity, R.layout.fragment_dth_main_spinner_items,userstories); 
     super(activity,resouceId,textviewId,data); 
     this.countryNames = data; 
    } 

    public class ViewHolder 
    { 
     TextView names; 
     ImageView icon; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(R.layout.fragment_dth_main_spinner_items, parent, false); 
    //  convertView = inflter.inflate(fragment_dth_main_spinner_items, null); 
     SpinnerMenu spinnerData = countryNames.get(position); 
     ViewHolder viewHolder; 
     View result; 

     if(convertView == null){ 
      viewHolder = new ViewHolder(); 
      viewHolder.icon = (ImageView) convertView.findViewById(R.id.imageView); 
      viewHolder.names = (TextView) convertView.findViewById(R.id.serviceName); 
      result = convertView; 
     } 
     else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
      result = convertView; 
     } 
//  viewHolder.icon.setImageBitmap(spinnerData.getImage()); 
//  Glide.with(context).load(spinnerData.getImages()).into(viewHolder.icon); 
     viewHolder.names.setText(spinnerData.getOpd()); 
     return convertView; 
    } 
} 

回答

0

讓您customadapter全球和的onComplete

public class PhotoCommnFragment extends android.support.v4.app.Fragment { 
    EditText rechargeMobileNumber, rechargeAmount; 
    Spinner selectMenu; 
    int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon}; 
    List<SpinnerMenu> selectedNetwork = new ArrayList<>(); 
    Fragment_DTH_Main_Spinner_Adapter customAdapter; 

    public PhotoCommnFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false); 
     mobileRecahrgeHistory(); 
     rechargeMobileNumber = (EditText) rootView.findViewById(R.id.recharge_mobile_number); 
     rechargeAmount = (EditText) rootView.findViewById(R.id.recharge_amount); 
     selectMenu = (Spinner) rootView.findViewById(R.id.selectNetwork); 
     settingSpinnerDropDown(); 
     customAdapter = new Fragment_DTH_Main_Spinner_Adapter(getActivity(), R.layout.fragment_dth_main_spinner_items, R.id.serviceName, selectedNetwork); 
     return rootView; 
    } 

    public void mobileRecahrgeHistory() { 
     Ion.with(this) 
       .load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1") 
       .asJsonObject().withResponse() 
       .setCallback(new FutureCallback<Response<JsonObject>>() { 
        @Override 
        public void onCompleted(Exception e, Response<JsonObject> result) { 
         JSONObject json = null; 

         try { 
          json = new JSONObject(result.getResult().toString()); 
         } catch (JSONException e1) { 
          e1.printStackTrace(); 
         } 
         // Create the root JSONObject from the JSON string. 
         JSONObject jsonRootObject = null; 
         jsonRootObject = json.optJSONObject("DS"); 

         //Get the instance of JSONArray that contains JSONObjects 
         JSONArray jsonArray = jsonRootObject.optJSONArray("LST"); 

         //Iterate the jsonArray and print the info of JSONObjects 
         for (int i = 0; i < jsonArray.length(); i++) { 
          JSONObject jsonObject = null; 
          try { 
           jsonObject = jsonArray.getJSONObject(i); 
          } catch (JSONException e1) { 
           e1.printStackTrace(); 
          } 
          String iph = null; 

          String oid = jsonObject.optString("OID").toString(); 
          String ocd = jsonObject.optString("OCD").toString(); 
          String opd = jsonObject.optString("OPE").toString(); 
          String mil = jsonObject.optString("MIL").toString(); 
          String mxl = jsonObject.optString("MXL").toString(); 
          try { 
           iph = jsonObject.getString("IPH").toString(); 
          } catch (JSONException e1) { 
           e1.printStackTrace(); 
          } 

          String urldisplay = "http://192.168.1.105/TotalRecharge/" + iph; 
          Bitmap mIcon11 = null; 
          try { 
           InputStream in = new java.net.URL(urldisplay).openStream(); 
           mIcon11 = BitmapFactory.decodeStream(in); 
          } catch (Exception e3) { 
           e3.printStackTrace(); 
          } 
          SpinnerMenu spinnerData = new SpinnerMenu(); 
          spinnerData.setOid(oid); 
          spinnerData.setOcd(ocd); 
          spinnerData.setOpd(opd); 
          spinnerData.setMil(mil); 
          spinnerData.setMix(mxl); 
          spinnerData.setImage(mIcon11); 

          selectedNetwork.add(spinnerData); 
          customAdapter.notifyDataSetChanged(); 

         } 

        } 

       }); 
    } 


    public void settingSpinnerDropDown() { 

     selectMenu.setAdapter(customAdapter); 
    } 
+0

定製適配器異步方法,但adapter.notifyDataSetChanged()後初始化被稱爲撥打notifydatasetchanged在異步方法中。這將導致空指針異常 –

+0

這將/不應該導致空指針。請試試 –

+0

仍然沒有解決問題,它的返回空值 –