gpt4 book ai didi

android - 无论我做什么,我在 fragment 中的 recyclerview 都不会更新

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:39 24 4
gpt4 key购买 nike

现在已经一周了,我真的很想找到解决方案,因为我需要在本周内向我的客户提交更新。因此,我在 fragment 中有一个 recyclerview,其中填充了使用 volley 库从 Web 服务获取的数据。如果将新数据添加到数据库中,我希望 recyclerview 重新加载/更新以实现更改或新添加的数据。现在,我尝试了很多方法,但未能实现我需要的。我尝试了以下方法:

-notifyDataSetChanged() 在我的适配器上,在处理程序内,在 onclick 内

-adapter.notifyItemRangeChanged(0, adapter.getItemCount());

-recyclerView.invalidate() 也在处理程序中- 分离和附加当前 fragment

-还尝试了以下代码。好吧,它之所以有效,是因为它刷新了整个 Activity 并返回到第一页的初始 fragment

Intent intent = getActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();

getActivity().overridePendingTransition(0, 0);
startActivity(intent);

fragment 类

public class AddPlaylist extends Fragment {

RecyclerView recyclerView;
RecyclerAdapter adapter;
String[] id,title;
ArrayList<String> artist;
TextView text;

CreatePlaylist createPlaylist;

private CoordinatorLayout coordinatorLayout;
private FloatingActionButton fab;
View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.addplaylist, container, false);

coordinatorLayout = (CoordinatorLayout) view.findViewById(R.id
.coordinatorLayout);

fab = (FloatingActionButton) view.findViewById(R.id.fab);

text = (TextView) view.findViewById(R.id.text);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInputDialog();
}
});

id = getArguments().getStringArray("id");
title = getArguments().getStringArray("title");

artist = new ArrayList<String>(Arrays.asList(title));

recyclerView= (RecyclerView) view.findViewById(R.id.my_recycler_view);
adapter = new RecyclerAdapter("addplaylist", id, artist, getActivity());
recyclerView.setAdapter(adapter);
recyclerView.invalidate();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

adapter.notifyItemRangeChanged(0, adapter.getItemCount());

final Handler mHandler = new Handler();

mHandler.post(new Runnable() {

@Override
public void run() {

adapter.notifyDataSetChanged();
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
recyclerView.invalidate();

Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("playlist");
Bundle bundle = new Bundle();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
addPlaylist.setArguments(bundle);
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(addPlaylist);
ft.attach(addPlaylist);
ft.commit();

}
});

return view;
}

protected void showInputDialog() {

// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(promptView);

final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//text.setText("Hello, " + editText.getText());
createPlaylist.createPlaylist(editText.getText().toString());

/*Intent intent = getActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();

getActivity().overridePendingTransition(0, 0);
startActivity(intent);*/
/*Bundle bundle = new Bundle();
Collections collections = new Collections();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
bundle.putString("fragment", "addfragment");
addPlaylist.setArguments(bundle);
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("playlist");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(collections);
ft.attach(frg);
ft.commit();*/



/*getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});*/

/*FragmentManager fragmentTransaction = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentTransaction.beginTransaction();
Bundle bundle = new Bundle();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
addPlaylist.setArguments(bundle);
transaction.addToBackStack("xyz");
transaction.add(R.id.containerView, addPlaylist);
//transaction.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
transaction.commit();*/

adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

@UiThread
protected void dataSetChanged() {
adapter.notifyDataSetChanged();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

id = getArguments().getStringArray("id");
title = getArguments().getStringArray("title");

artist = new ArrayList<String>(Arrays.asList(title));

adapter = new RecyclerAdapter("addplaylist", id, artist, getActivity());
adapter.notifyDataSetChanged();
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
createPlaylist = (CreatePlaylist) activity;
}catch(Exception e) {

}
}

public interface CreatePlaylist{

void createPlaylist(String name);

}
}

对不起,乱七八糟的代码。这是我都尝试过的。

最佳答案

发生这种情况是因为您的适配器“丢失了”。首先,您创建适配器。那挺好的。然后你得到新的数据。您所做的是创建另一个适配器,并更改第二个适配器中的数据,该适配器未附加到您的 RecyclerView。

这不是最优雅的解决方案,但它会起作用。在 onActivityResult 中更改这些行:

 artist = new ArrayList<String>(Arrays.asList(title));

adapter = new RecyclerAdapter("addplaylist", id, artist, getActivity());

List<String> newArtist = new ArrayList<String>(Arrays.asList(title));
artist.clear();
artist.addAll(newArtist);
adapter.notifyDataSetChanged();

因为你的列表 artist 是对象,当你将它传递给你的 Recycler 适配器时,你更新相同的实例,相同的列表,所以你不需要创建新的适配器,因为适配器仍然有访问同一实例。把它想象成一桶水。您可以清空桶或装满桶,但桶保持不变。

关于android - 无论我做什么,我在 fragment 中的 recyclerview 都不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35457584/

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