Java getSupportFragmentManager()在我的适配器类中不工作?

Java getSupportFragmentManager()在我的适配器类中不工作?,java,android,android-intent,Java,Android,Android Intent,我试图将当前片段中的意图添加到另一个片段中。为此,我在卡中的ImageButton上添加了一个OnclickListener 但它不能正常工作,我无法在我的代码中找到错误 package com.example.kgb.homescreen.myaccount; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.support.

我试图将当前片段中的意图添加到另一个片段中。为此,我在卡中的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();
        }
    }
}
package com.example.kgb.homescreen.myaccount;
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.Color;
导入android.support.v4.app.Fragment;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageButton;
导入com.example.kgb.R;
导入com.example.kgb.components.UpdatedTextView;
导入java.util.ArrayList;
公共类MyAccountScardapter扩展了RecyclerView.Adapter{
私有静态语境;
公共MyAccountScardapter(上下文){
this.context=context;
}
私人ArrayList CardArrayList;
公共MyAccountScarDapter(ArrayList cardData){
this.cardaraylist=cardData;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutFlater.from(parent.getContext()).flate(R.layout.accounts\u card\u布局,parent,false);
ImageButton imButton=(ImageButton)itemView.findViewById(R.id.shareButton);
返回新的ViewHolder(itemView,imButton);
}
@凌驾
BindViewHolder上的公共无效(MyAccountsCardAdapter.ViewHolder,int位置){
//holder.accountBalance.setTextColor(Color.parseColor(#000000”);
Log.d(“ABBB”,“haii”+holder.accountType.getText());
if(holder.accountType.getText().equals(“Savings”))
{
holder.accountBalance.setTextColor(Color.parseColor(#000000”);
}
holder.accountNo.setText(cardaraylist.get(position.getAccountNo());
holder.scheme.setText(cardArrayList.get(position.getScheme());
holder.accountType.setText(cardArrayList.get(position.getAccountType());
holder.accountBalance.setText(cardArrayList.get(position.getAccountBalance());
}
@凌驾
public int getItemCount(){return cardarayList.size();}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
公共图像按钮;
UpdatedTextView accountNo=(UpdatedTextView)itemView.findViewById(R.id.acc_no);
UpdatedTextView方案=(UpdatedTextView)itemView.findViewById(R.id.acc_方案);
UpdatedTextView accountType=(UpdatedTextView)itemView.findViewById(R.id.acc_类型);
UpdatedTextView accountBalance=(UpdatedTextView)itemView.findViewById(R.id.acc_bal);
公共视图持有者(查看项目视图,图像按钮嵌入按钮){
超级(项目视图);
setOnClickListener(这个);
iButton=imButton;
context=itemView.getContext();
}
@凌驾
公共void onClick(视图v){
Fragment MFFragment=新的CardExpandFragment();
context.getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand,mFragment.commit();
}
}
}

只需将活动传递给适配器并使用

activity.getSupportFragmentManager()

但这不是个好主意。在适配器中使用侦听器,并在设置适配器的活动中调用它。

onClick
操作移动到相应的
片段/活动
,其中通过使用
接口使用
适配器
。最好不要在
适配器类
中执行
片段事务

如你所见:

itemView.setOnClickListener(这个)


此代码行表示,
图像按钮
没有特定的
onClickListener
。您设置的
单击侦听器
用于整个项目。

最好将
碎片管理器
从您的活动传递给适配器构造函数(如果您正在使用碎片,则从碎片传递)


只是不要在ViewHolder中指定上下文值。您已经在从构造函数传递上下文值

   public ViewHolder(View itemView, ImageButton imButton) {

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

    }
您只需要将您的上下文强制转换为AppcompatActivity。只需确保您使用的是AppCompatActivity而不是Activity,并在适配器的构造函数中从Activity传递上下文值。而不是只有它能铸造

    @Override
    public void onClick(View v) {

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

对于那些使用kotlin的用户,您可以简单地执行以下操作

val dialog = IntervModifFragment()//The fragment that u want to open for example
       val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
        dialog.show(ft, ContentValues.TAG) 

你调用
Intent
。为什么
Fragment
同一页首先,最好在主页中处理片段事务。第二,正如@IntelliJAmiya所说,为什么你要同时启动一个新活动和一个新片段事务?我实际上忘记删除Intent部分了……我刚刚正确地更新了代码now@IntelliJAmiya dude
getSupportFragmentManager
getFragmentManager
FragmentActivity
Activity
中的方法,这些方法在
上下文中不可用。
   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();
        ((AppCompatActivity)context).getSupportFragmentManager().beginTransaction().replace(R.id.cardExpand, mFragment).commit();
    }
val dialog = IntervModifFragment()//The fragment that u want to open for example
       val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
        dialog.show(ft, ContentValues.TAG)