从async执行异步任务的方法获取数组列表中的数据

时间:2014-06-26 05:33:58

标签: android arraylist android-asynctask

我使用asynck task获取json并解析它。已解析json的值我将其存储在ArrayList中。现在我想要的是我希望我们在不同的班级中Arraylist。我已经制定了一个方法getList()来获取Arraylist,但我得到null值< / p>

异步类

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

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

    public interface getArrayList {
        public void getList(ArrayList<SearchModel> data);

    }

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

                    }

                }
                if (values.size () > 0) {
                   list.getList (values);
                } else {

                }


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

使用界面

的类
public class SearchJobList extends ListFragment implements SearchJobAsync.getArrayList {
    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, this);


    }

    @Override
    public void getList(ArrayList<SearchModel> data) {
        customList = new SearchJobCustomList (c, data);
        setListAdapter (customList);
    }
} 

logcat的

 java.lang.NullPointerException
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:83)
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:25)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method) 

现在如何获取arrayList ?????我把它变成了静止的,因为它不是正确的方式。所以请帮助我

2 个答案:

答案 0 :(得分:0)

我认为你在doInBackground完成工作之前试图获得价值。在AsyncTask任务中使用一些布尔静态标志,并在onPostExecute中更新。在获得价值之前检查那个标志。这是一个简单的解决方法。否则你必须使用界面。

答案 1 :(得分:0)

这是一个示例代码块,用于返回Asynctask的字符串结果,同样可以为您的情况做。

public interface AsyncResponseHandler {
    public String resultCall(String resultStr);
}

public class MyMainClass extends Activity implements AsyncResponseHandler{

    public void doProcessing(){
        new AsynTasker(this).execute(null); //here this is instance of AsyncResponseHandler
    }

    @Override
    public String resultCall(String resultStr) {
        //here you will receive results from your async task after execution and you can carry out whatever process you want to do on data received.
    }
}

public class AsynTasker extends AsyncTask<String,Void,String>{
    AsyncResponseHandler handler=null;
    public AsynTasker(AsyncResponseHandler handler){
      this.handler = handler
    }

    @Override
    protected String doInBackground(String... params) {
         // do your processing
         return resultString;
    }

    @Override
    protected void onPostExecute(String result) {
        this.handler.resultCall(result);
    }

}