我试图搜索有关此主题的任何讨论,但是到目前为止,我还没有发现任何有用的东西。因此,我决定继续进行发布。
所以我的询问是关于Android最佳做法的。我正在创建一个简单的应用程序,该应用程序调用RESTful端点,解析下载的JSON,并在活动中包含的某些片段中显示结果。
我有一个扩展AsyncTask的自定义“实用程序”类。在doInBackground方法中,我发出请求,并将响应存储在String等中(非常简单的东西)。
现在,我了解了AsyncTask的另外两个方法-onPreExecute和onPostExecute。如果我在线研究的内容是正确的,则可以通过将上下文传递给该实用工具类,通过id查找视图,设置字段文本或我想要的任何方式来与UI进行交互。去做。
这就是我的问题。围绕此的最佳实践是什么?
当前,我的扩展AsyncTask并进行Web服务调用的实用程序类具有一个私有成员变量:
private FragmentActivity mActivityContext;
然后,为了与UI进行交互,我需要执行以下操作:
@Override
protected void onPreExecute()
{
super.onPreExecute();
android.support.v4.app.FragmentManager fm = mActivityContext.getSupportFragmentManager();
MainActivity.PlaceholderFragment fragment = (MainActivity.PlaceholderFragment)fm.findFragmentByTag("PlaceholderFragment");
if (fragment != null)
{
TextView textView = (TextView)fragment.getView().findViewById(R.id.my_custom_field);
textView.setText("This field was updated through onPreExecute!");
}
}
尽管到目前为止这似乎还行,但是我担心的是-实用程序类是否应该访问它所引用的活动的片段管理器?它甚至应该提及该活动吗?通过使用片段管理器,实用程序类必须知道片段标记。这不是一个非常模块化或松散耦合的方法。解决方案是否像创建实用程序类的实例时传入片段标签一样简单,而不是像现在这样进行硬设置?还是有更好的替代路线?
这是我做的,我是从StackOverflow中学到的:
可以说我们正在从服务器数据库下载类别
首先,创建一个界面
public interface OnCategoriesDownloadedListener {
public void onCategoriesDownloadSuccess(ArrayList<String> categories);
public void onCategoriesDownloadFailure(String reason);
}
然后,在等待下载这些类别的活动中,我实现此接口:
public class MyActivity extends Activity implements OnCategoriesDownloadedListener {
@Override
onCategoriesDownloadSuccess(ArrayList<String> categories) {
//get the categories and do whatever my soul desire
}
@Override
onCategoriesDownloadFailure(String reason) {
//get the reason and come up with appropriate failure excuse
}
}
在我的asynctask类中,我执行以下操作
public class GetCategoriesFromServerAsync extends AsyncTask<String, String, ArrayList<String>> {
private OnCategoriesDownloadedListener listener;
private ArrayList<String> categoryNamesArray;
private String reason;
public GetCategoriesFromServerAsync(OnCategoriesDownloadedListener listener) {
this.listener = listener;
//usually I pass more data here, for example when Im registering a user in the server database, Im using the constructor to pass their data.
}
@Override
protected ArrayList<String> doInBackground(String... args) {
int success;
try {
//JSON stuff
return categoryNames;
} else if (success == 2) {
reason = "failure";
return null;
} else {
reason = "Connection error";
return null;
}
} catch (JSONException e) {
e.printStackTrace();
reason = "Connection error";
return null;
}
}
@Override
protected void onPostExecute(Object[] categoryNamesAndLogos) {
if (listener != null) {
if (categoryNames!=null) {
listener.onCategoriesDownloadSuccess(categoryNames);
} else if (reason.contentEquals(TAG_FAILURE)) {
listener.onCategoriesDownloadFailure(reason);
} else if (reason.contentEquals(TAG_CONNECTION_ERROR)) {
listener.onCategoriesDownloadFailure(reason);
}
}
super.onPostExecute(categoryNames);
}
}
从实现侦听器的活动中,我使用其构造函数启动asynctask:
new GetCategoriesFromDatabase(this).execute();
因此,请注意该活动正在实现侦听器并正在等待结果。
在asynctask postExecute中,我只处理结果,将结果发送到正在等待结果的活动中,并通过onCategoriesDownloadSuccess方法处理它们
如果要显示加载程序,则可以在从活动开始asynctask并结束在onSuccess和onFailure方法中显示它的同时进行。此时,我在AsyncTask的onPreExecute和onPostExecute中执行此操作,但是我需要一些时间或建议,然后思考哪种方法更好。
不确定这是否是最佳做法,因为这是我所知道的唯一方法,但肯定对我有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句