gpt4 book ai didi

java - getSupportFragmentManager() 在我的适配器类中不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:40 26 4
gpt4 key购买 nike

我正在尝试将当前 fragment 的 Intent 添加到另一个 fragment 。为此,我在卡片中的 ImageButton 上添加了一个 OnclickListener。

但它不能正常工作,我找不到我的代码中的错误。

package com.example.kgb.homescreen.myaccount;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.example.kgb.R;
import com.example.kgb.components.UpdatedTextView;

import java.util.ArrayList;


public class MyAccountsCardAdapter extends RecyclerView.Adapter<MyAccountsCardAdapter.ViewHolder> {

private static Context context;

public MyAccountsCardAdapter(Context context) {
this.context=context;
}

private ArrayList<MyAccountsCard>cardArrayList;
public MyAccountsCardAdapter(ArrayList<MyAccountsCard> cardData) {
this.cardArrayList=cardData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.accounts_card_layout, parent, false);
ImageButton imButton = (ImageButton) itemView.findViewById(R.id.shareButton);
return new ViewHolder(itemView,imButton);
}

@Override
public void onBindViewHolder(MyAccountsCardAdapter.ViewHolder holder, int position ){
//holder.accountBalance.setTextColor(Color.parseColor("#000000"));

Log.d("ABBB","haiii" + holder.accountType.getText());
if(holder.accountType.getText().equals("Savings"))
{
holder.accountBalance.setTextColor(Color.parseColor("#000000"));
}
holder.accountNo.setText(cardArrayList.get(position).getAccountNo());
holder.scheme.setText(cardArrayList.get(position).getScheme());
holder.accountType.setText(cardArrayList.get(position).getAccountType());
holder.accountBalance.setText(cardArrayList.get(position).getAccountBalance());
}

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

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public ImageButton iButton;
UpdatedTextView accountNo=(UpdatedTextView) itemView.findViewById(R.id.acc_no);
UpdatedTextView scheme=(UpdatedTextView) itemView.findViewById(R.id.acc_scheme);
UpdatedTextView accountType=(UpdatedTextView) itemView.findViewById(R.id.acc_type);
UpdatedTextView accountBalance=(UpdatedTextView) itemView.findViewById(R.id.acc_bal);
public ViewHolder(View itemView, ImageButton imButton) {

super(itemView);
itemView.setOnClickListener(this);
iButton = imButton;
context = itemView.getContext();

}

@Override
public void onClick(View v) {

Fragment mFragment = new CardExpandFragment();
context.getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand, mFragment).commit();
}
}
}

最佳答案

最好将 FragmentManager 从您的 Activity (或者如果您使用的是 fragment )传递给您的适配器构造函数

FragmentManager fragmentManager;
public MyAccountsCardAdapter(Context context ,ArrayList<MyAccountsCard> cardData ,FragmentManager fragmentManager) {
this.cardArrayList=cardData;
this.fragmentManager = fragmentManager;
}

然后调用

Fragment mFragment = new CardExpandFragment();
fragmentManager .beginTransaction().replace(R.id.cardExpand, mFragment).commit();

关于java - getSupportFragmentManager() 在我的适配器类中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41441539/

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