在arrayList上获取空值

时间:2014-06-25 13:19:37

标签: android arraylist

我将jSon数组数据存储在ArrayList<Model>中。我想在其他类中使用此ArrayList<Model>,因此我创建了一个返回ArrayList的方法。但总是我变得无效。

public class SearchJobAsync extends AsyncTask<String, String, String> {
    private String response;
    Context c;
    SearchModel data;
    ArrayList<SearchModel> values;

    public SearchJobAsync(Context c) {
        this.c = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute ();
        CommonFunctions.showProgress (c, "Please Wait...", true);

    }

    @Override
    protected void onPostExecute(String s) {
        values = new ArrayList<SearchModel> ();

        super.onPostExecute (s);
        if (!s.trim ().contains ("Table")) {
            Crouton.makeText ((android.app.Activity) c, "Nothing found", Style.INFO).show ();
        } else {
            try {

                JSONObject jsonObject = new JSONObject (s);
                JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                if (NewDataSet.get ("Table") instanceof JSONObject) {
                    JSONObject table = NewDataSet.getJSONObject ("Table");
                    data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                    values.add (data);
                } else if (NewDataSet.get ("Table") instanceof JSONArray) {
                    JSONArray tableArray = NewDataSet.getJSONArray ("Table");

                    for (int i = 0; i < tableArray.length (); i++) {
                        JSONObject table = tableArray.getJSONObject (i);
                        data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                        values.add (data);

                    }

                }


            } catch (JSONException e) {
                e.printStackTrace ();
            }
        }

        CommonFunctions.showProgress (c, "", false);
        Intent i = new Intent (c, SearchJobListActivity.class);
        c.startActivity (i);
    }

    @Override
    protected String doInBackground(String... s) {
        response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/FindJobForVendor").send ("Vendor_IEntity_Code=" + "34588A34-E969-4723-84FE-E5409B66A5B7" + "&Job_Code=" + "&Job_Category=1" + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=0" + "&Exp_Months_To=0").body ();
        response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
        Log.e ("Search Jobs", "" + response);
        return response;
    }

    public ArrayList<SearchModel> getList(){
        return values;
    }
}

我正在使用arraylist的类

public class SearchJobList extends ListFragment {
    private View view;
    private ListView lvSearchJobs;
    private ArrayList<SearchModel> data;
    SearchJobCustomList customList;
    SearchJobAsync searchJobAsync;
    private Context c;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate (R.layout.search_job_lists, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated (savedInstanceState);
        c = getActivity ();
        lvSearchJobs = (ListView) getActivity ().findViewById (android.R.id.list);
        data = new ArrayList<SearchModel> ();
        searchJobAsync = new SearchJobAsync (c);
        data = searchJobAsync.getList ();

        customList = new SearchJobCustomList (c, data);
        setListAdapter (customList);

    }
}

我在使用.execute()方法的类

public class SearchJobsFragment extends Fragment implements View.OnClickListener {

    private View view;
    private Spinner spCategory;
    private Button btSearch;
    private Spinner spToYrs;
    private Spinner spFromMonths;
    private Spinner spFromYrs;
    private Spinner spToMonths;
    private EditText etJobCode;
    private String[] yrsArray, monthArray, professionArray;
    private ArrayAdapter<String> toYrs, toMonths, profession;
    private Context c;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate (R.layout.search_jobs_frag, container, false);
        return view;
    }

    private void initialize() {

        c = getActivity ();
        spCategory = (Spinner) getActivity ().findViewById (R.id.sp_category);
        btSearch = (Button) getActivity ().findViewById (R.id.bt_search);
        spToYrs = (Spinner) getActivity ().findViewById (R.id.sp_to_yrs);
        spFromMonths = (Spinner) getActivity ().findViewById (R.id.sp_from_months);
        spFromYrs = (Spinner) getActivity ().findViewById (R.id.sp_from_yrs);
        spToMonths = (Spinner) getActivity ().findViewById (R.id.sp_to_months);
        etJobCode = (EditText) getActivity ().findViewById (R.id.et_job_code);
        btSearch.setOnClickListener (this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated (savedInstanceState);
        initialize ();
        spinnerSetup ();
    }

    private void spinnerSetup() {

        //for to/from yrs

        yrsArray = getResources ().getStringArray (R.array.Experience_yrss);
        toYrs = new ArrayAdapter<String> (c, R.layout.spinner_textview, yrsArray);
        toYrs.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spToYrs.setAdapter (toYrs);
        spFromYrs.setAdapter (toYrs);


        // for to/from months
        monthArray = getResources ().getStringArray (R.array.Experience_months);
        toMonths = new ArrayAdapter<String> (c, R.layout.spinner_textview, monthArray);
        toMonths.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spToMonths.setAdapter (toMonths);
        spFromMonths.setAdapter (toMonths);


        //for category

        professionArray = getResources ().getStringArray (R.array.Profession);
        Arrays.sort (professionArray, 1, professionArray.length);
        profession = new ArrayAdapter<String> (c, R.layout.spinner_textview, professionArray);
        profession.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spCategory.setAdapter (profession);


    }

    @Override
    public void onClick(View view) {
        SearchJobAsync searchJobAsync = new SearchJobAsync (c);
        searchJobAsync.execute ();
    }
}

getList()方法中,我将返回值,但它始终显示为null。

3 个答案:

答案 0 :(得分:0)

如果您在onPostExecute()被执行之前尝试访问,则列表将为空。因为它尚未初始化。因此,在尝试访问列表之前,请确保完成任务。

在显示进度时,最好将本地List<SearchModel>引用传递给SearchJobAsync的构造函数。只有在隐藏对话框后,此引用才有值。 因此添加一个构造函数,如:

public SearchJobAsync(Context c, List<SearchModel> values) {
    this.c = c;
    this.values=values;
}

在你的活动中

List<SearchModel> myListVar;
new SearchJobAsync(MyActivity.this, myListVar);

答案 1 :(得分:0)

您在任务完成之前调用getList方法(在任务完成后调用方法onPostExecute)。当列表准备就绪时,您必须通知您的上下文(活动,片段)。

答案 2 :(得分:0)

问题在于以下几行

 data = new ArrayList<SearchModel> ();
 searchJobAsync = new SearchJobAsync (c);
 data = searchJobAsync.getList ();

您没有为AsyncTask调用execute()方法,如下所示

 new DownloadFilesTask().execute(url1, url2, url3);

如果没有此onPreExecute,则不会调用doInBackground和onPostExecute方法。这是您将数据作为null的原因。

还有一个建议,在你的onPostExecute()中,你正在调用startActivity()方法

 Intent i = new Intent (c, SearchJobListActivity.class);
    c.startActivity (i);

但是你想要SearchJobList片段中的ArrayList值。 AsyncTask成功执行后,屏幕上将显示新活动。你需要解决这个问题。

检查AsyncTask

的文档