【问题标题】:Created an interface To get Arraylist but still getting null创建了一个接口来获取 Arraylist 但仍然为空
【发布时间】:2014-06-30 06:50:48
【问题描述】:

在我的应用程序中,我想在其他类中使用异步类中的 ArrayList。因此,我创建了一个接口。但是当我在其他类中使用该接口时,我将 ArrayList 的大小设为 0。arraylist我在其他类中使用的是将其提供给自定义适配器以显示列表。但是由于我的 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;

    }
}

【问题讨论】:

  • 为什么在 onPostExecute() 中启动 SearchJobListActivity ?
  • @Haresh 那么我该从哪里开始???
  • 您要做的是在 SearchJobList 片段上执行 SearchJobAsync,当数据来自响应时,尝试在您的 SearchJobList 片段上显示结果是它还是其他?
  • 是的,你是对的,但是为了让数据显示在列表中,我想要异步类中的 ArrayList
  • 不要在 onPostExecute() 中启动 SearchJobListActivity 使用 onItemClickListener 进行列表视图并在 setListAdapter (customList) 处初始化您的适配器;在 onPostExecute()...

标签: java android interface android-asynctask


【解决方案1】:

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

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

【讨论】:

  • @Anuj Simply values = getArrayList.getList();.
  • 先生,我照你说的做了,但我得到空指针异常
  • @Anuj 很难在没有看到调整后的代码的情况下提出建议。在SearchJobList.onActivityCreated() 之前调用SearchJobList.getList() 的任何可能性?如果发生这种情况,您的列表为空。
【解决方案2】:

试试这个方法,希望能帮助你解决问题。

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

}

【讨论】:

  • 我按照你说的做了,但我不会去 ListActivity,它仍然在同一页面上
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 2022-12-15
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 2019-10-09
相关资源
最近更新 更多