gpt4 book ai didi

android - 需要解释如何将数据从适配器发送到 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:09:03 25 4
gpt4 key购买 nike

我想将数据从适配器发送到已经打开的 fragment 。在主要 Activity 中,已经打开了两个 fragment , fragment A 和 fragment B。在 fragment A 中,我放置了 RecyclerView 和 CardView。单击其中一个卡片 View 时,我想将其数据发送到 fragment B。调用 mCallbacks 时出现 NPE。

if (mCallbacks != null){
mCallbacks.onItemClicked(strStockName);}

在这种情况下,我使用了一个接口(interface)。但我需要更多解释。

这是我的适配器代码:

public class adapterStock extends RecyclerView.Adapter<adapterStock.ViewHolder> {
private String[] stockNameDataSet;
private Context mContext;

//==============================================
//---- interface
public interface Callbacks{
public void onItemClicked(String StockName);
}
private Callbacks mCallbacks;

//==============================================


public adapterStock(Context context, String[] DataSet){
stockNameDataSet = DataSet;
mContext = context;
}

@NonNull
@Override
public adapterStock.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
View view = layoutInflater.inflate(R.layout.cv_stock, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final String strStockName = stockNameDataSet[position];
holder.tbxStockName.setText(stockNameDataSet[position]);

holder.cardView.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {

Snackbar.make(view, "Stock "+strStockName, Snackbar.LENGTH_LONG).show();

if (mCallbacks != null){
mCallbacks.onItemClicked(strStockName);
}

}
});
}

@Override
public int getItemCount() {
return stockNameDataSet.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
public View view;
public TextView tbxStockName;
public CardView cardView;

public ViewHolder(View v){
super (v);
tbxStockName = (TextView) v.findViewById(R.id.cvTbxItemStockName);
cardView = (CardView) v.findViewById(R.id.cvStock);
}
}

public void setCallbacks(Callbacks callbacks){
this.mCallbacks = callbacks;
}
}

这是 fragment A:

public class AFrag extends Fragment implements adapterStock.Callbacks {
private String strStockName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_A, container, false);
tbxStock = (TextView) view.findViewById(R.id.tbxR);
return view;
}

@Override
public void onItemClicked(String StockName) {
Toast.makeText(getActivity(), "Stock terpilih " + StockName, Toast.LENGTH_LONG).show();
tbxStock.setText(strStockName);
}
}

欢迎提出建议。谢谢。

最佳答案

一旦创建了 adapterStock 实例,就在 fragment A 中包含 setCallbacks(this)。

关于android - 需要解释如何将数据从适配器发送到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601724/

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