gpt4 book ai didi

java - 保存 arrayList 而不是覆盖抽屉导航中的每个项目单击?

转载 作者:行者123 更新时间:2023-11-30 02:09:56 26 4
gpt4 key购买 nike

我有一个抽屉导航,其中包含 HomeNewsPhotos 等项目列表。当我单击一个项目时,会加载并显示相应的 fragment 。

假设如果我点击 News,它会执行 Async Task 并将数据保存在 ArrayList 中。我的问题是,如果我在抽屉导航中再次点击 News 项目,它会删除旧的 ArrayList 值并显示新项目(即覆盖)。

由于我是 fragment 的新手,所以我不知道如何保存我的旧 ArrayList 值。

主 Activity .java

DemoFragment fragment = new DemoFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();

DemoFragment.java

public class DemoFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<Post> postArrayList;
PostRecyclerAdapter postRecyclerAdapter;
ProgressWheel progressWheel;

public DemoFragment() {

// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_post, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.posts_list);
// progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
progressWheel = (ProgressWheel) v.findViewById(R.id.progress_wheel);

recyclerView.setHasFixedSize(true);

// use a linear layout manager
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);


new LoadPosts(getActivity(), progressWheel, recyclerView).execute();
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

setRetainInstance(true);


}

}

LoadPosts.java

class LoadPosts extends AsyncTask<Void, Void, Void>
{
ProgressDialog progressDialog;
List<ParseObject> parseObjects;
Context context;
FragmentManager supportFragmentManager;
ArrayList<Post> postArrayList;
String postC, postT,postI;
RecyclerView recyclerView;
ProgressWheel progressWheel;


public LoadPosts(FragmentActivity context, ProgressWheel progressWheel, RecyclerView recyclerView) {

this.context = context;
this.progressWheel = progressWheel;
this.recyclerView = recyclerView;

}


@Override
protected void onPreExecute() {
super.onPreExecute();

progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
//progressDialog.show();
progressWheel.setVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... avoid) {

postArrayList = new ArrayList<>();
// dont care about postC, postT. I get these values from parse db
Post post = new Post();
post.setContent(postC);
post.setCreatedAt(postT);
postArrayList.add(post);


return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

progressWheel.setVisibility(View.GONE);
PostRecyclerAdapter postRecyclerAdapter = new PostRecyclerAdapter(context, postArrayList);
recyclerView.setAdapter(postRecyclerAdapter);

supportFragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}

}

最佳答案

您需要将来自 onCreateView 的数据 postArrayList 返回给调用者。请记住,当您调用 ASyncTask 时,会创建/使用一个 UI 线程,之后所有数据都会从内存中释放(在某个时间点)。

更改自:

public LoadPosts(FragmentActivity context, ProgressWheel progressWheel, RecyclerView recyclerView)

TO(例如,更改第三个参数):

public LoadPosts(FragmentActivity context, ProgressWheel progressWheel, ArrayList<Post> postArrayList)

在 AsyncTask 中,更改 FROM:

@Override
protected void onPostExecute(String result) {

收件人:

@Override
protected void onPostExecute(ArrayList<Post> postArrayList) {
super.onPostExecute(postArrayList);

return postArrayList;
...
}

注意事项:

  • 基本上,我们的想法是将修改后的数据返回给您的 Fragment 类。我实际上从来没有这样做过。我认为 ASyncTask 是为在后台做事而构建的,然后忘记结果。
  • Google 网页说

onPostExecute(Result), invoked on the UI thread after the background computation finishes.

,链接@ ASyncTask .搜索文本。

尝试一下,让我们知道...

关于java - 保存 arrayList 而不是覆盖抽屉导航中的每个项目单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30240788/

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