RecyclerView 中的数据未从 Fragment 传递到适配器

创建 a 后RecyclerView,我注意到我在 aBundle中声明的字符串没有从 传递Fragment到AlertDialog,并且连接的文本视图显示为空白。有谁知道出了什么问题?我是否需要为此场景或其他场景使用意图?


在片段中使用:


Bundle bundle = new Bundle();

bundle.putString("goalreached",  getResources().getString(R.string.goal_reached));

bundle.putString("day",  getResources().getString(R.string.today));

bundle.putString("distance",  getResources().getString(R.string._3_2_km));

bundle.putString("calories",  getResources().getString(R.string._213_kcal));

用于适配器:


Bundle bundle = new Bundle();

String goalreached = bundle.getString("goalreached");   

String day = bundle.getString("day");

String distance = bundle.getString("distance");                        

String calories = bundle.getString("calories");

片段类


public class TabFragmentRV extends android.support.v4.app.Fragment {

    RecyclerView mRecyclerView;


    public TabFragmentRV() {}


    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_rv, container, false);

    }


    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        View v = getView();

        assert v != null;


        mRecyclerView = v.findViewById(R.id.my_recyclerview);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));


        Bundle bundle = new Bundle();

        bundle.putString("goalreached",  getResources().getString(R.string.goal_reached));

        bundle.putString("day",  getResources().getString(R.string.today));

        bundle.putString("distance",  getResources().getString(R.string._3_2_km));

        bundle.putString("calories",  getResources().getString(R.string._213_kcal));


        super.onActivityCreated(savedInstanceState);


        initRVAdapter();

 

    }

}

富国沪深
浏览 136回答 2
2回答

肥皂起泡泡

您需要将第一个创建的包传递到您的适配器中(可能是在创建时)这是具有必要更改的类public class RVItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {&nbsp; &nbsp; private final static int TYPE_TABLE = 1, TYPE_SEPARATOR = 2;&nbsp; &nbsp; private ArrayList myArrayList = new ArrayList();&nbsp; &nbsp; private Context context;&nbsp; &nbsp; //Added Global bundle&nbsp; &nbsp; Private Bundle mBundle;&nbsp; &nbsp; public RVItemsAdapter(Context context){&nbsp; &nbsp; &nbsp; &nbsp; this. context=context;&nbsp; &nbsp; }&nbsp; &nbsp; //Added Constructor&nbsp; &nbsp; public RVItemsAdapter(Context context, Bundle bundle){&nbsp; &nbsp; &nbsp; &nbsp; this.context=context;&nbsp; &nbsp; &nbsp; &nbsp; this.mBundle = bundle;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public void setCallSMSFeed(List<Object> myArrayList){&nbsp; &nbsp; &nbsp; &nbsp; this.myArrayList = (ArrayList) myArrayList;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemViewType(int position) {&nbsp; &nbsp; &nbsp; &nbsp; if (myArrayList.get(position) instanceof TableRV) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return TYPE_TABLE;&nbsp; &nbsp; &nbsp; &nbsp; } else if (myArrayList.get(position) instanceof RVLineSeparator) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return TYPE_SEPARATOR;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Item at position " + position + " is not an instance of either Phonecall or SMSmessage");&nbsp; &nbsp; }&nbsp; &nbsp; //Update Bundle&nbsp; &nbsp; public void updateView(Bundle bundle){&nbsp; &nbsp; &nbsp; &nbsp; mBundle = bundle;&nbsp; &nbsp; &nbsp; &nbsp; this.notifyDataSetChanged();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; int viewType=holder.getItemViewType();&nbsp; &nbsp; &nbsp; &nbsp; switch (viewType){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TYPE_TABLE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableRV tblRV = (TableRV) myArrayList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((TblViewHolder)holder).bind(tblRV);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TYPE_SEPARATOR:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((SeparatorViewHolder)holder).showSeparatorDetails();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("unexpected viewType: " + viewType);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount(){return myArrayList.size();}&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; int layout;&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.ViewHolder viewHolder;&nbsp; &nbsp; &nbsp; &nbsp; switch (viewType){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TYPE_TABLE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layout = R.layout.cardview_tableview_withexpandability;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View tblView = LayoutInflater&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .from(parent.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(layout, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewHolder = new TblViewHolder(tblView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TYPE_SEPARATOR:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layout = R.layout.lineseparatorforrecyclerview;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View separatorView = LayoutInflater&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .from(parent.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(layout, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewHolder = new SeparatorViewHolder(separatorView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("unexpected viewType: " + viewType);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return viewHolder;&nbsp; &nbsp; }&nbsp; &nbsp; public class TblViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; final Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);&nbsp; &nbsp; &nbsp; &nbsp; private Button btnToday, btnYesterday, btnThisWeek, btnThisMonth;&nbsp; &nbsp; &nbsp; &nbsp; private TextView arrowexpandcollapseTextView, sectionNameTextView;&nbsp; &nbsp; &nbsp; &nbsp; TblViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnMale.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.setButton(Dialog.BUTTON_NEUTRAL,"OK",new DialogInterface.OnClickListener(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = LayoutInflater.from(context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressLint("InflateParams") View content = inflater.inflate(R.layout.dialog_stats, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.setView(content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Pulling String out of bundle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String goalreached = mBundle.getString("goalreached");&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String day = mBundle.getString("day");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String distance = mBundle.getString("distance");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String calories = mBundle.getString("calories");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageView imgdialogMain = content.findViewById(R.id.imgView_genderA);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ivGenderA.setImageResource(R.drawable.ic_male);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvGoalReached = content.findViewById(R.id.txtView_dialog_goalreached);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvGoalReached.setText(goalreached);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvDay = content.findViewById(R.id.txtView_day);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvDay.setText(day);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvImgviewWalking = content.findViewById(R.id.imgView_walking);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvDistance = content.findViewById(R.id.txtView_distance);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvDistance.setText(distance);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvImgviewFire = content.findViewById(R.id.imgView_fire);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvCaloriesBurned = content.findViewById(R.id.txtView_location);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvCaloriesBurned.setText(calories);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}目前,您只是在片段中创建一个新包并向其中添加字符串,然后什么也不做,然后在适配器中创建另一个新包并尝试拉出不存在的字符串。要更新数据,您可以在需要更新数据时在要调用的适配器中尝试类似的操作(不过,我不确定 notifyDataSetChanged() 是否可以在适配器内部工作)://Update Bundlepublic void updateView(Bundle bundle){&nbsp; &nbsp; mBundle = bundle;&nbsp; &nbsp; this.notifyDataSetChanged();}

弑天下

您正在尝试从这里的新捆绑包中获取额外内容:Bundle bundle = new Bundle();String goalreached = bundle.getString("goalreached");...您需要获取存储该信息的包。捆绑包只是信息的“容器”,因此您需要存储它然后检索它。您永远不会将捆绑包保存在任何地方,因此您以后无法阅读。我不确定你想在这里做什么,所以我不能给你一个例子,但看看这个:Bundle bundle = new Bundle();bundle.putString("goalreached",&nbsp; getResources().getString(R.string.goal_reached));bundle.putString("day",&nbsp; getResources().getString(R.string.today));bundle.putString("distance",&nbsp; getResources().getString(R.string._3_2_km));bundle.putString("calories",&nbsp; getResources().getString(R.string._213_kcal));您正在创建该捆绑包,然后存储信息,仅此而已。你永远不会把它存放在任何地方。捆绑包的基本用法是将其存储在意图上,然后检索该意图并获取捆绑包。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java