我正在开发一个基于 fragment 的应用程序。这些 fragment 的很多内容都是使用 AsyncTask
从数据库中收集的。因此,我试图将“获取数据”类外部化,以便它可以被重用。我的 fragment 如下:
public class LocalFragment extends SherlockListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
LoadDataFromURL url_data = new LoadDataFromURL();
url_data.setContext(getSherlockActivity());
url_data.setURL("http://url.com/get_data/");
url_data.execute();
}
}
我的LoadDataFromURL
类如下:
class LoadDataFromURL extends AsyncTask<String, String, String>{
String our_url;
ListActivity our_context;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray results = null;
List<PubListDetails> pubs = new ArrayList<PubListDetails>();
Handler mHandler;
public void setContext(ListActivity context){
our_context = context;
}
public void setURL(String url){
our_url = url;
}
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mHandler = new Handler();
pDialog = new ProgressDialog(our_context);
pDialog.setMessage("Loading pubs please wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONArray json = jParser.makeHttpRequest(our_url, "GET", params);
Log.d("All Products: ", json.toString());
try {
int success = json.length();
if (success != 0){
results = json;
pubs.clear();
for (int i = 0; i <results.length(); i++){
JSONObject c = results.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String town = c.getString("town");
String county = c.getString("county");
//HashMap<String, String> map = new HashMap<String, String>();
pubs.add(new PubListDetails(id,name,town,county));
//map.put(TAG_PID, id);
//map.put(TAG_NAME, name);
//productsList.add(map);
}
}else{
Intent i = new Intent(our_context,MainMenu.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
our_context.startActivity(i);
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(our_context, "ff", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
pDialog.dismiss();
our_context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new PubListAdapter(our_context, pubs);
our_context.setListAdapter(adapter);
}});
}
}
根据 getSherlockActivity
没有传递 setListAdapter
要求的 ListActivity
给我错误。
独立于此,我越来越觉得我在概念上错过了重点,这不是实现我想要的目标的理想方式。
有人可以建议我如何从 fragment 中调用外部 AsyncTask
来收集数据,然后用它填充 ListView
吗?
最佳答案
首先,您要在 SherlockListFragment
上设置数据,这样您的业务就不会与 Activity
相关。
Independent of this, I am feeling more and more that I have conceptually missed the point and this isn't the ideal way to achieve what I want.
Could someone advise how from a fragment I can call an external AsyncTask which will collect data and then populate a list view with it..?
简单实现一个回调接口(interface)。
public interface OnLoadDataListener {
public onLoadComplete(List<PubListDetails> data);
}
让你的 fragment 类实现这个接口(interface):
public class LocalFragment extends SherlockListFragment implements OnLoadDataListener {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LoadDataFromURL url_data = new LoadDataFromURL();
url_data.setContext(getSherlockActivity());
url_data.setURL("http://url.com/get_data/");
url_data.setListener(this); // you could unite the setContext() method in the listener registration
url_data.execute();
}
@Override
public void onLoadComplete(List<PubListDetails> data) {
ListAdapter adapter = new PubListAdapter(getSherlockActivity(), pubs);
our_context.setListAdapter(adapter);
}
}
在你的AsyncTask
中:
private OnLoadDataListener mListener
public void setListener(OnLoadDataListener listener){
mListener = listener;
}
并在 onPostExecute
中将数据发送给监听器:
@Override
protected void onPostExecute(String result) {
Toast.makeText(our_context, "ff", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
pDialog.dismiss();
if (mListener != null) {
mListener.onLoadComplete(pubs);
}
}
关于android - 外部 AsyncTask 从 Fragment 填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13473995/