gpt4 book ai didi

java - 如何将数据从 fragment 传递到适配器

转载 作者:行者123 更新时间:2023-12-04 09:48:49 25 4
gpt4 key购买 nike

让我快速解释一下我的代码结构,我有一个带有动态 fragment 的动态选项卡布局。这些选项卡是从 json 响应中获取的,每个选项卡都有一个动态 fragment ,其中还包含一个从 json 响应填充的回收器 View 。

我正在尝试将所选选项卡的 ID 传递给回收站 View 的适配器

1. TransactionsFragment(创建选项卡布局的地方)

 for(int i=0;i<array.length();i++) {

//getting wallet object from json array
JSONObject userWallets=array.getJSONObject(i);

tab.addTab(tab.newTab().setText(userWallets.getString("wallet_name")));
walletID.add(userWallets.getInt("id"));
}

TransactionsPagerAdapter adapter = new TransactionsPagerAdapter
(getChildFragmentManager(), tab.getTabCount(), walletID); //the data i need to pass is the walletID
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab));
adapter.notifyDataSetChanged();

2. TransactionsPagerAdapter(控制选项卡的适配器)
public class TransactionsPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
ArrayList<Integer> walletID;

public TransactionsPagerAdapter(FragmentManager fm, int NumOfTabs, ArrayList<Integer> walletID) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.mNumOfTabs = NumOfTabs;
this.walletID = walletID;
}

@Override
public Fragment getItem(int position) {
DynamicFragment d = new DynamicFragment();
Bundle args = new Bundle();
args.putInt("your_key", walletID.get(position));
d.setArguments(args);
return d.newInstance(walletID.get(position));

}

@Override
public int getCount() {
return mNumOfTabs;
}
}

3. DynamicFragment(填充每个选项卡中的回收器 View )
for(int i=0;i<array.length();i++) {

//getting wallet object from json array
JSONObject userTransactions=array.getJSONObject(i);

//adding the wallet to wallet list
userTransactionList.add(new Transaction(
userTransactions.getInt("id"),
userTransactions.getInt("wallet_id"),
userTransactions.getDouble("fee"),
userTransactions.getDouble("amount"),
userTransactions.getDouble("from"),
userTransactions.getDouble("to"),
userTransactions.getString("destination_address"),
userTransactions.getString("type"),
userTransactions.getString("created_at")
));
}

//creating adapter object and setting it to recyclerview
TransactionsAdapter adapter = new TransactionsAdapter(getActivity(),childFragmentManager, userTransactionList);
mRecyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
// stop animating Shimmer and hide the layout
mShimmerViewContainer.stopShimmerAnimation();
mShimmerViewContainer.setVisibility(View.GONE);
// progressDialog.dismiss();
adapter.notifyDataSetChanged();

4. TransactionsAdapter(控制回收器 View 的适配器)
public class TransactionsAdapter extends RecyclerView.Adapter<TransactionsAdapter.DataObjectHolder> {

private static String TAG = TransactionsAdapter.class.getSimpleName();

private Context mCtx;
private FragmentManager fragmentManager;

private ArrayList<Transaction> userTransactionList;
private static MyClickListener myClickListener;

public TransactionsAdapter(Context mCtx, FragmentManager fragmentManager, ArrayList<Transaction> userTransactionList) {
this.mCtx = mCtx;
this.fragmentManager = fragmentManager;
this.userTransactionList = userTransactionList;
}

public static class DataObjectHolder extends RecyclerView.ViewHolder {
TextView transactionamount, transactionstatus, transactionid;
ImageView transactionicon, pendingicon;

public DataObjectHolder(View itemView) {
super(itemView);
transactionamount = (TextView) itemView.findViewById(R.id.amount);
transactionstatus = (TextView) itemView.findViewById(R.id.status);
transactionicon = (ImageView) itemView.findViewById(R.id.tx_icon);
pendingicon = (ImageView) itemView.findViewById(R.id.img_status);
transactionid = (TextView) itemView.findViewById(R.id.wallet_id);
}

}

@Override
public TransactionsAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.transaction_item, parent, false);
final TransactionsAdapter.DataObjectHolder dataObjectHolder = new TransactionsAdapter.DataObjectHolder(view);
return dataObjectHolder;
}


@Override
public void onBindViewHolder(TransactionsAdapter.DataObjectHolder holder, final int position) {

DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.##");
formatter.setDecimalSeparatorAlwaysShown(true);
formatter.setMinimumFractionDigits(2);
Double doubleBalance = userTransactionList.get(position).getTransactionAmount();
String numberFormatBalance = formatter.format(doubleBalance);
holder.transactionstatus.setText(userTransactionList.get(position).getTransactionType());
holder.transactionamount.setText(String.valueOf("₦ " + numberFormatBalance));
if ((userTransactionList.get(position).getTransactionType()).equals("send")) {
holder.transactionicon.setImageResource(R.drawable.ic_communication_call_made);
} else {
holder.transactionicon.setImageResource(R.drawable.ic_communication_call_received);
}

}


@Override
public int getItemCount() {
return userTransactionList.size();
}

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

我需要从 1(TransactionsFragment) 到 4(TransactionsAdapter) 获取 walletID,这是否可能,如果是这样,我知道事实上这不能用 bundle 完成,因为我们没有创建 Intent

最佳答案

  • 确保您将 walletId 设置为 ArgumentsDynamicFragment来自
  • DynamicFragment.newInstance(walletID.get(position))
  • 修改 TransactionsAdapter 的构造函数关注此
  • private Integer walletId;

    public TransactionsAdapter(Context mCtx, FragmentManager fragmentManager, ArrayList<Transaction> userTransactionList, Integer walletId) {
    this.mCtx = mCtx;
    this.fragmentManager = fragmentManager;
    this.userTransactionList = userTransactionList;
    this.walletId = walletId;
    }


  • DynamicFragment当你初始化 TransactionsAdapter获取 walletId来自 fragment 的参数
  • Integer walletId = getArguments().getInt("your_key") // your key when you newInstance and put to Bundle
    TransactionsAdapter adapter = new TransactionsAdapter(getActivity(),childFragmentManager, userTransactionList);

    最后,您的适配器具有 walletId 的信息

    关于java - 如何将数据从 fragment 传递到适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62047035/

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