2014-06-25 18 views
0

我已经存储了一个ArrayList.I中的json数组数据。我想在其他类中使用这个ArrayList。我可以如何实现。我是通过使它成为静态的,但它不是一个好方法做它。我也做了哪些西港岛线返回的ArrayList的方法,但每次我渐渐空value.What shud我做在其他类中使用arraylist从一个类

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

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

    public SearchJobAsync(Context c, ArrayList<SearchModel> values) { 
     this.c = c; 
     this.values = values; 
    } 

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

ListFragment类

public class SearchJobList extends ListFragment { 
    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); 
     data = searchJobAsync.getList(); 

     customList = new SearchJobCustomList (c, data); 
     setListAdapter (customList); 

    } 
} 

ListFragment活性状况TY

public class SearchJobListActivity extends FragmentActivity { 
    private FragmentTransaction fragmentTransaction; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate (savedInstanceState); 
     setContentView (R.layout.main_with_headers); 
     initialize(); 
    } 

    private void initialize() { 
     fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     SearchJobList searchJobList = new SearchJobList(); 
     fragmentTransaction.replace (R.id.container, searchJobList, "searchJobList").commit(); 
    } 
} 

类,其中I M调用异步.execute()方法

public class SearchJobsFragment extends Fragment implements View.OnClickListener { 

    private View view; 
    private Spinner spCategory; 
    private Button btSearch; 
    private Spinner spToYrs; 
    private Spinner spFromMonths; 
    private Spinner spFromYrs; 
    private Spinner spToMonths; 
    private EditText etJobCode; 
    private String[] yrsArray, monthArray, professionArray; 
    private ArrayAdapter<String> toYrs, toMonths, profession; 
    private Context c; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     view = inflater.inflate (R.layout.search_jobs_frag, container, false); 
     return view; 
    } 

    private void initialize() { 

     c = getActivity(); 
     spCategory = (Spinner) getActivity().findViewById (R.id.sp_category); 
     btSearch = (Button) getActivity().findViewById (R.id.bt_search); 
     spToYrs = (Spinner) getActivity().findViewById (R.id.sp_to_yrs); 
     spFromMonths = (Spinner) getActivity().findViewById (R.id.sp_from_months); 
     spFromYrs = (Spinner) getActivity().findViewById (R.id.sp_from_yrs); 
     spToMonths = (Spinner) getActivity().findViewById (R.id.sp_to_months); 
     etJobCode = (EditText) getActivity().findViewById (R.id.et_job_code); 
     btSearch.setOnClickListener (this); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated (savedInstanceState); 
     initialize(); 
     spinnerSetup(); 
    } 

    private void spinnerSetup() { 

     //for to/from yrs 

     yrsArray = getResources().getStringArray (R.array.Experience_yrss); 
     toYrs = new ArrayAdapter<String> (c, R.layout.spinner_textview, yrsArray); 
     toYrs.setDropDownViewResource (android.R.layout.simple_list_item_single_choice); 
     spToYrs.setAdapter (toYrs); 
     spFromYrs.setAdapter (toYrs); 


     // for to/from months 
     monthArray = getResources().getStringArray (R.array.Experience_months); 
     toMonths = new ArrayAdapter<String> (c, R.layout.spinner_textview, monthArray); 
     toMonths.setDropDownViewResource (android.R.layout.simple_list_item_single_choice); 
     spToMonths.setAdapter (toMonths); 
     spFromMonths.setAdapter (toMonths); 


     //for category 

     professionArray = getResources().getStringArray (R.array.Profession); 
     Arrays.sort (professionArray, 1, professionArray.length); 
     profession = new ArrayAdapter<String> (c, R.layout.spinner_textview, professionArray); 
     profession.setDropDownViewResource (android.R.layout.simple_list_item_single_choice); 
     spCategory.setAdapter (profession); 


    } 

    @Override 
    public void onClick(View view) { 
     SearchJobAsync searchJobAsync = new SearchJobAsync (c); 
     searchJobAsync.execute(); 
    } 
} 

应该怎么做什么访问其他类也ArrayList中??

+0

其他类是一个Activity类吗? – Raghunandan

+0

无其他班级是ListFragment.See我已编辑我的帖子 – Anuj

+0

ListFragment附加到活动。而asynctask是从活动中调用的?您也可以从片段本身调用asynctask并更新onPostExecute – Raghunandan

回答

0

您可以将arraylist包装在单独的单例类中。

http://en.wikipedia.org/wiki/Singleton_pattern

并且关于列表是空.....

在这条线:

searchJobAsync = new SearchJobAsync (c); 

我想你可能意味着启动的AsyncTask所以应该如下所示:

searchJobAsync = new SearchJobAsync (c); 
searchJobAsync.execute(); 

回复评论主题(以保持这个类简单的,我结合创造和获得在同一代码的单身,不过这将工作):

public class DataHolder(){ 
ArrayList<SearchModel> list; 
private static DataHolder instance; 

private DataHolder(ArrayList<SearchModel> list){ 
this.list = list; 
} 

public getInstance(ArrayList<SearchModel> list){ 
if(instance == null){ 
instance = new DataHolder(list); 
} 
return instance; 
} 

public ArrayList<SearchModel> getList(){ 
return list 
} 

} 

,然后在的AsyncTask获取数据持有人的新实例与列表:

DataHolder.getInstance(list); 
+0

什么是我创建的getList()方法?为什么我得到null – Anuj

+0

@Anuj我编辑了我的答案。让我知道它是怎么回事。 –

+0

先生的异步。execute()方法我在diffrnt中使用Activity和diffrnt中的arrylist活动 – Anuj

0

你应该使用observer模式并处理你的fragment类中的结果。只要你modifiy的AsyncTask如下:

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

SearchDataListener searchDataListener; 

public static interface SearchDataListener { 
    void onSearchSuccess(List<SearchModel> data); 
    void onSearchFailed(); 
} 

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

public void setSearchDataListener(SearchDataListener searchDataListener) { 
    this.searchDataListener = searchDataListener; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    CommonFunctions.showProgress (c, "Please Wait...", true); 
} 

@Override 
protected void onPostExecute(String s) { 
    List<SearchModel> 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); 
       } 

      } 
      // call the listener here 
      if(values.size() > 0) {    
       searchDataListener.onSearchSuccess(values); 
      } else 
       searchDataListener.onSearchFailed(); 
      } 
     } 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; 
} 
} 

,并调用它在你的片段的阶级是这样的:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated (savedInstanceState); 
    c = getActivity(); 
    lvSearchJobs = (ListView) getActivity().findViewById (android.R.id.list); 
    searchJobAsync = new SearchJobAsync (c); 
    searchJobAsync.setSearchListener(new SearchListener(){ 
     @Override 
     public void onSearchSuccess(List<SearchModel> data) { 
      // handler result 
      customList = new SearchJobCustomList (c, data); 
      setListAdapter (customList); 
     } 
     @Override 
     public void onSearchFailed() { 
      // handler failure 
     } 
} 

希望这有助于!

+0

或先生我应该使用静态ArrayList <>和在其他类 – Anuj

+0

先生我收到空指针异常 – Anuj

+0

空指针异常在哪里发生? – Robin