gpt4 book ai didi

android - Livedata无法正确更新 fragment 中的数据

转载 作者:行者123 更新时间:2023-12-03 10:41:18 25 4
gpt4 key购买 nike

我在单独的选项卡中有tablayout和2 fragments
片段A有一个重写的方法,当Activity(从片段A开始)销毁数据时,该方法将返回数据:

public class Fragment A extends Fragment {
...
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode != RESULT_CANCELED) {
assert data != null;
String accountTransaction = data.getStringExtra("Account");
String categoryTransaction = data.getStringExtra("Category");
Double getDouble = data.getDoubleExtra("Value", 0);

TransactionNewItem item = new TransactionNewItem(String.valueOf(getDouble),accountTransaction,categoryTransaction);
model.setSelected(item);

}
super.onActivityResult(requestCode, resultCode, data);
}
}
在相同的方法中,我使用了对ViewModel的调用,该调用应遵守TransactionNewItem对象:
public class TransactionViewModel extends ViewModel {
private final MutableLiveData<TransactionNewItem> selected = new MutableLiveData<>();
public void setSelected (TransactionNewItem item){
selected.setValue(item);
}

public LiveData<TransactionNewItem> getSelected() {
return selected;
}
}
从Activity返回具有新值的数据后,它将创建一个新的POJO并将此POJO中存储的数据发送到Fragment B,在Fragment B的基础上,将为RecyclerView创建一个新项
public class Fragment B extends Fragment {
...
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initObserve();
initRecView();
super.onViewCreated(view, savedInstanceState);
}
//init RecyclerView
private void initRecView(){
binding.transactionView.setLayoutManager(new LinearLayoutManager(requireContext()));
adapter = new TransactionRecViewAdapter(listContentArr);
adapter.setListContent(listContentArr);
binding.transactionView.setAdapter(adapter);
}

//observe data from Fragment A and create object based on it
private void initObserve(){
model = new ViewModelProvider(requireActivity()).get(TransactionViewModel.class);
model.getSelected().observe(getViewLifecycleOwner(), item -> {
TransactionItem newAccountItem = new TransactionItem() ;
newAccountItem.setTransactionValue(item.getTransactionValue());
newAccountItem.setTransactionCategory(item.getTransactionCategory());
newAccountItem.setTransactionAccount(item.getTransactionAccount());
listContentArr.add(0,newAccountItem);
adapter.notifyDataSetChanged();
});
}
}
但是,它将仅向RecyclerView中添加一项,并在Activity返回新数据时将其替换为。如果用户至少一次没有切换到片段B,则会发生这种情况,因为在用户切换到片段B之前,不会调用onViewCreated。
如果用户以前从未切换到片段B,那么如何使ViewModel观察片段A的数据,并在Activity每次返回新数据时在片段B Recyclerview中创建新的TransActionItem?
提前致谢

最佳答案

编辑:我设法做我想要的下一步:
步骤1.使用POJO-ArrayList将ViewModel从POJO更改为Arraylist:

public class TransactionViewModel extends ViewModel {

private final MutableLiveData<ArrayList<TransactionItem>> selected = new MutableLiveData<>();

public void setSelected (ArrayList<TransactionItem> arrayList){
selected.setValue(arrayList);
}

public LiveData<ArrayList<TransactionItem>> getSelected() {
return selected;
}
}
步骤2。在片段A中,我在onActivityResult中添加了具有相同POJO类型的ArrayList。我更改了代码,现在将在Activity返回结果(而不是在片段B中)之后创建并添加对象:
public class Fragment A extends Fragment {
ArrayList<TransactionItem> listTransactions = new ArrayList<>();

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode != RESULT_CANCELED) {
...
//Create TransactionItem and use setSelected method from ViewModel
TransactionItem item = new TransactionItem(accountTransaction,
String.valueOf(getDouble),categoryAccount),transactionID);
listTransactions.add(0,item);
model.setSelected(listTransactions);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
必须注意,我已将transactionID添加到TransactionItem构造函数中,这就是我们需要它的原因。
步骤3我创建了下一个TransactionDiffUtilCallback类,该类扩展了DiffUtil.Callback:
public class TransactionDiffUtilCallback extends `DiffUtil.Callback` {

public TransactionDiffUtilCallback(ArrayList<TransactionItem> oldList, ArrayList<TransactionItem> newList) {
this.oldList = oldList;
this.newList = newList;
}

ArrayList<TransactionItem> newList;

@Override
public int getOldListSize() {
return oldList.size();
}

@Override
public int getNewListSize() {
return newList.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).getItemIID() == newList.get(newItemPosition).getItemIID();
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
}

@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {

return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
我使用POJO中的 getItemIID()通知ArrayList中的新项目不同。
步骤4在recyclerview适配器中,我创建了updateItemList(list):
public void updateItemList(ArrayList<TransactionItem> items){
final TransactionDiffUtilCallback diffCallback = new TransactionDiffUtilCallback(this.pad_list, items);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

this.pad_list.clear();
this.pad_list.addAll(items);
diffResult.dispatchUpdatesTo(this);
}
因此,此方法使用DiffUtil.CallBack比较Fragment A中的ArrayList和Fragment B中的ArrayList中的项目,然后通知适配器Fragment A中的ArrayList不同,并且此数据应放在Fragment B中的ArrayList中,并应更新 View 。
步骤5在片段B中,重写了OnViewCreated()代码以永远观察Arraylist:
 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initRecView();
model = new ViewModelProvider(requireActivity()).get(TransactionViewModel.class);
Observer<ArrayList<TransactionItem>> observer = (Observer<ArrayList<TransactionItem>>) this::initObserve;
model.getSelected().observeForever(observer);
super.onViewCreated(view, savedInstanceState);
}
并且initObserve()现在具有下一个代码:
private void initObserve(ArrayList<TransactionItem> list){
adapter.updateItemList(list);
}
目前,此解决方案正在运行,用户无需切换到片段B即可保留交易记录。我将继续测试该解决方案。

关于android - Livedata无法正确更新 fragment 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65871386/

25 4 0