Arraylist在异步方法中返回null?

时间:2017-08-16 01:45:44

标签: android arraylist android-asynctask

我面临的问题是来自服务器的JSON数据保存在ArrayList中,当在自定义适配器中调用ArrayList时返回null。自定义适配器用于列出android中微调器中的数据。

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中,但是当在 Fragment_DTH_Main_Spinner_Adapter`中调用arraylist时,arraylist返回null。我通过调试检查数据是否保存在arraylist中。 我在stackoverflow中考虑过你应该添加

方法中的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;
    }
}

1 个答案:

答案 0 :(得分:0)

使你的customadapter全局并在oncomplete中调用notifydatasetchanged

internal static void getCommand(string command, string arg)
{
    command == "debug";
}