创建一个接口获取Arraylist但仍然为null

时间:2014-06-30 06:50:49

标签: java android interface android-asynctask

在我的应用程序中,我想使用其他类中的异步类的ArrayList。因为我创建了一个接口。但是当我在其他类中使用该接口时,我将ArrayList的大小设置为0.The arraylist我在其他类中使用的是将它提供给自定义适配器以显示List.But,因为我的Arraylist大小为0,正在显示空白列表

异步类

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

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

    @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);
                    if(values.size()>0){
                        getArrayList.getList(values);
                    }
                } 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){
                            getArrayList.getList(values);
                        }

                    }

                }



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


    @Override
    public void getList(ArrayList<SearchModel> data) {
       values=data;
    }
}

接口

public interface GetArrayList  {

public void getList(ArrayList<SearchModel> data);
}

我使用ArrayList的类

public class SearchJobList extends ListFragment implements GetArrayList {
    private View view;
    private ListView lvSearchJobs;
    private ArrayList<SearchModel> value;
    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);
        value = new ArrayList<SearchModel> ();
        searchJobAsync = new SearchJobAsync (c,this);
        customList = new SearchJobCustomList (c, value);
        setListAdapter (customList);
//        searchJobAsync.execute ();


    }

    @Override
    public void getList(ArrayList<SearchModel> data) {
        value=data;

    }
}

2 个答案:

答案 0 :(得分:0)

您可能应该像这样定义您的界面:

public interface GetArrayList  {    
  List<SearchModel> getList();
}

然后你可以返回一个这样的列表对象:

@Override
public List<SearchModel> getList() {
    return Collections.unmodifiableList(value);
}

你当前的界面很奇怪,但是如果你的实现是合理的话,它可以工作。目前,您的getList()实现就像一个setter而不是getter。你可以这样做:

@Override
public void getList(ArrayList<SearchModel> data) {
    data.clear();
    data.addAll(value);
}

最后注意事项:GetArrayList是一个奇怪的接口名称。通常,接口应以描述性方式命名,而不是直接在它们持有的方法之后命名。例如,您可以调用界面ListHolderSearchModelCatalogue

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

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

    @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);
        lvSearchJobs = (ListView) getActivity ().findViewById (android.R.id.list);
        new SearchJobAsync(getActivity(),new GetArrayList() {
            @Override
            public void getList(ArrayList<SearchModel> data) {
                value = data;
                customList = new SearchJobCustomList (c, value);
                setListAdapter (customList);
            }
        }).execute();
    }

}

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

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

    @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, "Please Wait...",false);
            getArrayList.getList(values);
        }
    }

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

}