【发布时间】: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