【问题标题】:Async Task Fragment Background Data异步任务片段背景数据
【发布时间】:2019-10-31 13:28:50
【问题描述】:

对于如何从这个网站加载大量模板 JSON 数据到我的应用程序而不减慢主 UI 线程,我非常陌生和困惑。我可以将数据从 JSON 响应加载到我的回收站视图中,但是单击显示该数据的底部导航选项卡需要很长时间才能加载到页面上。

我知道这是因为数据没有正确加载到后台线程,这就是我感到困惑的地方。

我希望能够从后台任务中返回一个 ArrayList,并获取该填充的 ArrayList 并将其作为我的适配器传递给 recyclerview。这是包含 AsyncTask 和 recyclerview 代码的片段:

public class LoadJSONAsyncTask extends AsyncTask<String, String, ArrayList<String>>
    {

        protected void onPreExecute()
        {


        }

        public String getJSONFromURL(String inUrl) throws IOException
        {
            URL url2 = new URL(inUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
            StringBuilder total = new StringBuilder();

            try
            {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                BufferedReader r = new BufferedReader(new InputStreamReader(in));

                for(String line; (line = r.readLine()) != null;)
                {
                    total.append(line).append('\n');
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            finally
            {
                urlConnection.disconnect();
            }

            return total.toString();
        }

        @Override
        protected ArrayList<String> doInBackground(String... params)
        {
            LoadJSONAsyncTask jParser = new LoadJSONAsyncTask();
            JSONArray array = null;
            JSONObject json = null;
            String url = "https://baconipsum.com/api/?type=meat-and-filler";

            StringBuilder returnMe = new StringBuilder();
            results = new ArrayList<>();

            try {
                array = new JSONArray(jParser.getJSONFromURL(url));
                for(int n = 0; n < array.length(); n++)
                {
                    try {
                        json = new JSONObject();

                        json.put("name", jParser.getJSONFromURL(url));
                        results.add(jParser.getJSONFromURL(url));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



            return results;
        }

        protected void onPostExecute()
        {

        }

    }

在我的片段中的 onCreateView() 方法中,recyclerview 定义如下:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        try
        {
            results = new LoadJSONAsyncTask().execute(url).get();
            for(int i = 0; i < results.size(); i++)
            {
                newsFeedList.add(new NewsFeed(results.get(i)));
            }
        } catch (ExecutionException e) {

        } catch (InterruptedException e) {e.printStackTrace();
            e.printStackTrace();
        }
        View view = inflater.inflate(R.layout.fragment_feeds, container, false);

        newsFeedList = new ArrayList<>();

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        final NewsAdapter mAdapter = new NewsAdapter(getActivity(), newsFeedList);
        recyclerView.setAdapter(mAdapter);


        return view;
    }

我有一种感觉,在我的 onCreateView() 方法中,我调用 arraylist 来执行数据的方式是将数据挂在线程上的原因。一个清晰的解释和例子将不胜感激!

【问题讨论】:

    标签: java android android-recyclerview android-asynctask


    【解决方案1】:

    覆盖 LoadJSONAsyncTaskonPostExecute 并将所有 UI 更新都放在那里。

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
    
            View view = inflater.inflate(R.layout.fragment_feeds, container, false);
    
            newsFeedList = new ArrayList<>();
    
            recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    
            final NewsAdapter mAdapter = new NewsAdapter(getActivity(), newsFeedList);
            recyclerView.setAdapter(mAdapter);
    
            LoadJSONAsyncTask asyncTask = new LoadJSONAsyncTask() {
            @Override
            void onPostExecute(ArrayList<String> results) {
                    // update your adapter here with the result
    
                    for(int i = 0; i < results.size(); i++)
                    {
                        newsFeedList.add(new NewsFeed(results.get(i)));
                    }
    
                    // notify adapter here to update the view
                    mAdapter.notifyDataSetChanged()
                }
            }
    
            asyncTask.execute(url);
    
            return view;
        }
    
    

    .get() 挂断你的主线程。

    以下是正确使用AsyncTask 的方法。 https://developer.android.com/reference/android/os/AsyncTask

    【讨论】:

    • 要不要在onCreateView方法里做上面的代码?
    • 我已经更新了我的答案,所以它显示了你应该做什么。
    • 谢谢,我明天早上起来看看,非常感谢!
    【解决方案2】:

    LoadJSONAsyncTask 结束后调用 Fragment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多