猿问

片段中的 onPostExecute 输出

我有一个工作 AsyncTask 从服务器获取数据并将其显示在 TextView 中。但是是否可以将数据输出到位于片段中的 TextView 中?因此,假设 AsyncTask 在 MainActivity 中加载,输出将在一个片段中。


这是我的异步任务:


 private static class FtpDownload extends AsyncTask<String, String, String> {


        private WeakReference<GuidanceActivity> activityWeakReference;


        FtpDownload(GuidanceActivity activity) {

            activityWeakReference = new WeakReference<>(activity);

        }


        @Override

        protected void onPreExecute() {

            super.onPreExecute();

        }


        @Override

        protected String doInBackground(String... FTPconnection) {


            GuidanceActivity activity = activityWeakReference.get();

            if (activity == null || activity.isFinishing()) {

                return null;

            }


            try {

                FTPClient ftpClient = new FTPClient();

                ftpClient.connect("", 21);

                System.out.println(ftpClient.getReplyString());


                ftpClient.enterLocalPassiveMode();

                ftpClient.login("anonymous", "");

                ftpClient.changeWorkingDirectory("/");


                InputStream inStream = ftpClient.retrieveFileStream(".html");


                activity.contents = IOUtils.toString(inStream, StandardCharsets.UTF_8);

                System.out.println(activity.contents);


                ftpClient.disconnect();


            } catch (IOException e) {

                e.printStackTrace();

            }

            return null;

        }

        @Override

        protected void onPostExecute(String output) {


            GuidanceActivity activity = activityWeakReference.get();

            if (activity == null || activity.isFinishing()) {

                return;

            }

            TextView textView = activity.findViewById(R.id.text_view);

            textView.setText(Html.fromHtml(activity.contents));

        }

    }


绝地无双
浏览 143回答 2
2回答

莫回无

在您的片段中:public class MyFragment extends Fragment {&nbsp;&nbsp; &nbsp;TextView myTextView;&nbsp; &nbsp;@Nullable&nbsp; &nbsp;@Override&nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp;super.onCreateView(inflater, container, savedInstanceState);&nbsp; &nbsp; &nbsp;View view = View root = inflater.inflate(R.layout.my_frag_layout, null);&nbsp; &nbsp; &nbsp;myTextView = (TextView) view.findViewById(R.id.myTextView);&nbsp; &nbsp; &nbsp;//rest of your implementation of onCreateView&nbsp; &nbsp; &nbsp;return view;&nbsp; &nbsp;}&nbsp; &nbsp;//this will be called from activity&nbsp; &nbsp;public void updateTextView(String text) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myTextView.setText(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}}在您的活动中:public class MyActivity extends AppCompatActivity {&nbsp;&nbsp; &nbsp;private final String FRAGMENT_TAG = "myFragment";&nbsp; &nbsp;private static class FtpDownload extends AsyncTask<String, String, String> {&nbsp; &nbsp; //rest of your FtpDownload class&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(String output) {&nbsp; &nbsp; &nbsp; &nbsp; GuidanceActivity activity = activityWeakReference.get();&nbsp; &nbsp; &nbsp; &nbsp; if (activity == null || activity.isFinishing()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG );&nbsp; &nbsp; &nbsp; &nbsp; /* or&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.fragment );&nbsp; &nbsp; &nbsp; &nbsp; if your fragment is in an xml layout */&nbsp; &nbsp; &nbsp; &nbsp; fragment.updateTextView(Html.fromHtml(activity.contents));&nbsp; &nbsp; }&nbsp; }}

缥缈止盈

对的,这是可能的。这是来自 MainActivity 的 WiFi 扫描示例,结果显示在片段中。我相信您可以理解逻辑并将其转换为您的项目主要活动:public class MainActivity extends AppCompatActivity implements FragmentDiscoverWiFi.ScanWiFi;//public variableFragmentDiscoverWiFi fragmentDiscoverWiFi;//This will be called from your fragment@Overridepublic void onScanWiFi(FragmentDiscoverWiFi fragment) {&nbsp; &nbsp; &nbsp; &nbsp; fragmentDiscoverWiFi = fragment;}//use something like this when your want to update fragmentfragmentDiscoverWiFi.onScanWiFiComplete();分段:public class FragmentDiscoverWiFi extends Fragment {&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; public interface ScanWiFi {&nbsp; &nbsp; &nbsp; &nbsp; public void onScanWiFi(FragmentDiscoverWiFi fragment);&nbsp; &nbsp; }&nbsp; &nbsp; public FragmentDiscoverWiFi() {&nbsp; &nbsp; &nbsp; &nbsp; // Required empty public constructor&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onAttach(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; mContext = context;&nbsp; &nbsp; &nbsp; &nbsp; super.onAttach(context);&nbsp; &nbsp; }&nbsp; &nbsp; public void onScanWiFiComplete() {&nbsp; &nbsp; &nbsp; &nbsp; if (!isDetached()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Access your data from MainActivity like this:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "Total APs:" + ((MainActivity) mContext).wifiScanResults.size());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }@Overridepublic void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; if (!getUserVisibleHint()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; pullToRefreshText = rootView.findViewById(R.id.pullToRefreshText);&nbsp; &nbsp; &nbsp; &nbsp; pullToRefresh = rootView.findViewById(R.id.pullToRefresh);&nbsp; &nbsp; &nbsp; &nbsp; pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onRefresh() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "refreshing...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pullToRefresh.setRefreshing(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((MainActivity)mContext).onScanWiFi(FragmentDiscoverWiFi.this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答