Java 创建了一个接口以获取Arraylist,但仍然获取null
在我的应用程序中,我想在其他类中使用异步类中的ArrayList。为此,我创建了一个接口。但当我在其他类中使用该接口时,我得到的ArrayList的大小为0。我在其他类中使用的ArrayList将其提供给自定义适配器,以便在列表中显示。但由于我的ArrayList大小为0,正在显示空白列表 异步类Java 创建了一个接口以获取Arraylist,但仍然获取null,java,android,interface,android-asynctask,Java,Android,Interface,Android Asynctask,在我的应用程序中,我想在其他类中使用异步类中的ArrayList。为此,我创建了一个接口。但当我在其他类中使用该接口时,我得到的ArrayList的大小为0。我在其他类中使用的ArrayList将其提供给自定义适配器,以便在列表中显示。但由于我的ArrayList大小为0,正在显示空白列表 异步类 public class SearchJobAsync extends AsyncTask<String, String, String> implements GetArrayList
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;
}
}
公共类SearchJobAsync扩展AsyncTask实现GetArrayList{
私有字符串响应;
上下文c;
搜索模型数据;
数组列表值;
GetArrayList GetArrayList;
public SearchJobAsync(上下文c,GetArrayList GetArrayList){
这个.c=c;
this.getArrayList=getArrayList;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
CommonFunctions.showProgress(c,“请稍候…”,true);
}
@凌驾
受保护的void onPostExecute(字符串s){
values=newarraylist();
super.onPostExecute(s);
如果(!s.trim().包含(“表”)){
Crouton.makeText((android.app.Activity)c,“找不到任何东西”,Style.INFO.show();
}否则{
试一试{
JSONObject JSONObject=新的JSONObject;
JSONObject NewDataSet=JSONObject.getJSONObject(“NewDataSet”);
if(NewDataSet.get(“表”)JSONObject实例){
JSONObject table=NewDataSet.getJSONObject(“table”);
数据=新的搜索模型(table.getString(“工作类别”)、table.getString(“最低经验”)、table.getString(“最高经验”)、table.getString(“发布日期”)、table.getString(“候选人计数”)、table.getString(“应用程序”)、table.getString(“职位编号”)、table.getString(“工作描述”)、table.getString(“工作类型”)、table.getString(“工作小时数”),table.getString(“工作状态”)、table.getString(“工作日期”)、table.getString(“地址”)、table.getString(“性别名称”)、table.getString(“宗教名称”)、table.getString(“经验总结”)、table.getString(“IJob_请求ID”)、table.getString(“请求者名称”);
values.add(数据);
如果(values.size()>0){
getArrayList.getList(值);
}
}else if(NewDataSet.get(“Table”)JSONArray实例){
JSONArray tableArray=NewDataSet.getJSONArray(“表格”);
for(inti=0;i0){
getArrayList.getList(值);
}
}
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
CommonFunctions.showProgress(c,“,false);
意向i=新意向(c,SearchJobListActivity.class);
c、 星触觉(i);
}
@凌驾
受保护的字符串背景(字符串…s){
响应=HttpRequest.post(“https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/FindJobForVendor发送(“供应商业务代码=“+”34588A34-E969-4723-84FE-E5409B66A5B7“+”&作业代码=“+”&作业类别=1“+”&经验年从=0“+”&经验月从=0“+”&经验年到=0“+”&经验月到=0”)。正文();
response=response.replaceAll(“]*>”,“”)。replaceAll(“\n”,“”);
Log.e(“搜索作业”,“响应”);
返回响应;
}
@凌驾
公共void getList(ArrayList数据){
数值=数据;
}
}
接口
public interface GetArrayList {
public void getList(ArrayList<SearchModel> data);
}
公共接口GetArrayList{
公共void getList(ArrayList数据);
}
类中使用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;
}
}
公共类SearchJobList扩展ListFragment实现GetArrayList{
私人视野;
私有ListView lvSearchJobs;
私有数组列表值;
SearchJobCustomList自定义列表;
SearchJobAsync SearchJobAsync;
私有上下文c;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.search\u job\u list,container,false);
返回视图;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
c=getActivity();
lvSearchJobs=(ListView)getActivity().findviewbyd(android.R.id.list);
value=newarraylist();
searchJobAsync=新的searchJobAsync(c,this);
customList=新的SearchJobCustomList(c,值);
setListAdapter(自定义列表);
//searchJobAsync.execute();
}
@凌驾
公共void getList(ArrayList数据){
值=数据;
}
}
您可能应该这样定义界面:
public interface GetArrayList {
List<SearchModel> getList();
}
@Override
public List<SearchModel> getList() {
return Collections.unmodifiableList(value);
}
最后注意:
GetArrayList
对于接口来说是一个奇怪的名称。通常,接口应该以描述性的方式命名,而不是直接以它们所持有的方法命名。例如,您可以调用界面列表持有者
或搜索模型目录
尝试这种方式,希望这将帮助您解决问题。
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;
}
}
公共类SearchJobList扩展了ListF
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;
}
}