我正在查看 this answer而且它似乎只处理一个 TextView 。
基本上,我有一个包含 n
fragment 的 Android 应用程序,每个 fragment 都有一个通过远程调用数据库填充的 TextView 。每次选择 fragment 时,都会触发该远程调用,并且应重新填充 TextView 。
目前,我正在使用中央 AsyncTask 来完成此操作,但是我开始怀疑这样做是否是正确的方法(有些 TextView 需要很长时间才能更新少量数据,有些则不会得到更新,等等)。
这是我的 RetrieveData
类中的代码。本质上,它确定要更新哪个 TextView ,然后填充该 TextView 。
public class RetrieveData extends AsyncTask<String, String, String[]> {
private int txtViewID = -1;
private Activity mainActivity;
public RetrieveData(Activity a) { mainActivity = a; }
protected String[] doInBackground(String... urls) {
String[] data;
// call web script to return JSON data
...
// figure out which fragment called which script
if (urls[0] == "get_A.php") {
data = parseJSONdata(); // parse out the JSON
txtViewID = R.id.txtViewA; // find INT-based ID
} else if (urls[0] == "get_B.php") {
data = parseOtherJSONdata(); // different type of call
txtViewID = R.id.txtViewB;
} else ... {
...
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
return data;
}
@Override
protected void onPostExecute(String[] op) {
if (txtViewID != -1) { // call was made
TextView tv = (TextView)mainActivity.findViewById(txtViewID);
tv.setText(op[0]);
}
下面是我如何从 fragment 中调用它:
public class MainFragment extends Fragment {
Activity mainActivity;
public MainFragment(Activity a) { mainActivity = a; }
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.main_tab,container,false);
new RetrieveData(mainActivity).execute("get_A.php","1");
return v;
}
}
对我来说,它非常笨拙并且可能掩盖了我对 Android 的陌生感,因此非常感谢任何改进建议。
最佳答案
您可以做几件事来提高稳健性和性能,并修复一些稍后会出现的问题:
不要在 init/setup 类型方法之外使用
findViewById()
。这是一个昂贵的调用,因为它必须在您的层次结构中“搜索”您请求的 ID。不要为采用
Activity
的Fragment
使用重载的构造函数。Fragment
默认构造函数应该为空。这允许系统在配置更改(屏幕旋转)时正确地重新创建您的Fragment
。Fragment
将在正确的位置接收其附加的Activity
它的onAttach()
方法被调用的时间,所以没有必要这样做。您根本不需要
Activity
来完成您想要做的事情。相反,让您的Fragment
在其onCreateView()
中从您的布局中获取正确的TextView
。你从那里做什么完全取决于你:将
TextView
实例作为要更新的实例传递给您的RetrieveData
类构造函数。这消除了RetrieveData
类中的硬编码 ID,从而摆脱了一些显式耦合,是一种更好的方法。不过,这仍然是紧密耦合的,因为它依赖于特定的View
,所以恕我直言,这仍然不是一个很好的选择。让
RetrieveData
类定义内部Callback
接口(interface)并让Fragment
实现它。RetrieveData
的构造函数然后可以采用Callback
接口(interface)的实例(例如,您的Fragment
实例)并且当它的onPostExecute()
运行它只是用适当的数据回调Fragment
。现在由您的Fragment
实现来正确决定它托管的 UI 元素以使用数据更新。它现在可能是一个TextView
,但在未来您可以将它变成其他东西,等等。现在您已经将该类与所有显式 UI 联系解耦,并将责任放在承载 UI 元素的事物上:fragment
。
这是第二个项目符号的简要示例:
public RetrieveData extends AsyncTask<String, String, String[]> {
// Define the interface used to provide results
public interface Callback {
public void onDataLoaded(String[] result);
}
private Callback mCb;
public RetrieveData(Callback cb) {
mCb = cb;
}
...
@Override
public void onPostExecute(String[] result) {
mCb.onDataLoaded(result);
}
}
public MyFragment extends Fragment implements RetrieveData.Callback {
TextView mResult;
RetrieveData mAsyncRetriever;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.main_tab,container,false);
// Get the TextView now where we want to show results.
// This avoids calling findViewById() constantly.
mResult = (TextView)root.findViewById(R.id.example_result);
...
}
@Override
public void onResume() {
// Keep a reference to the AsyncTask so we can properly
// cancel it when our lifecycle events dictate so.
mAsyncRetriever = new RetrieveData(this);
mAsyncRetriever.execute("get_A.php");
}
@Override
public void onPause() {
// If we have a pending data load going on, kill it.
if (mAsyncRetriever != null) {
mAsyncRetriever.cancel(true);
mAsyncRetriever = null;
}
}
@Override
public void onDataLoaded(String[] result) {
// Only pulling the first result provided
mResult.setText(result[0]);
// The RetrieveData is done, get rid of our ref
mAsyncRetriever = null;
}
}
关于android - 为来自 AsyncTask 的不同 fragment 更新 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31168812/