gpt4 book ai didi

java - 异步任务 fragment 后台数据

转载 作者:行者123 更新时间:2023-12-02 09:46:07 26 4
gpt4 key购买 nike

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

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

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

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()
{

}

}

在 fragment 的 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 来执行数据的方式是将数据卡在线程上的。清晰的解释和示例将不胜感激!

最佳答案

重写 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() 挂起主线程。

以下是如何正确使用AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask

关于java - 异步任务 fragment 后台数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56640024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com