第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

RecyclerView 中的數(shù)據(jù)未從 Fragment 傳遞到適配器

RecyclerView 中的數(shù)據(jù)未從 Fragment 傳遞到適配器

富國滬深 2022-01-06 17:15:46
創(chuàng)建 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();     }}
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

您需要將第一個創(chuàng)建的包傳遞到您的適配器中(可能是在創(chuàng)建時)


這是具有必要更改的類


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

    private final static int TYPE_TABLE = 1, TYPE_SEPARATOR = 2;

    private ArrayList myArrayList = new ArrayList();

    private Context context;

    //Added Global bundle

    Private Bundle mBundle;


    public RVItemsAdapter(Context context){

        this. context=context;

    }


    //Added Constructor

    public RVItemsAdapter(Context context, Bundle bundle){

        this.context=context;

        this.mBundle = bundle;

    } 


    public void setCallSMSFeed(List<Object> myArrayList){

        this.myArrayList = (ArrayList) myArrayList;

    }


    @Override

    public int getItemViewType(int position) {

        if (myArrayList.get(position) instanceof TableRV) {

            return TYPE_TABLE;

        } else if (myArrayList.get(position) instanceof RVLineSeparator) {

            return TYPE_SEPARATOR;

        }

        throw new IllegalArgumentException("Item at position " + position + " is not an instance of either Phonecall or SMSmessage");

    }


    //Update Bundle

    public void updateView(Bundle bundle){

        mBundle = bundle;

        this.notifyDataSetChanged();

    }


    @Override

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        int viewType=holder.getItemViewType();

        switch (viewType){

            case TYPE_TABLE:

                TableRV tblRV = (TableRV) myArrayList.get(position);

                ((TblViewHolder)holder).bind(tblRV);

                break;

            case TYPE_SEPARATOR:

                ((SeparatorViewHolder)holder).showSeparatorDetails();

                break;

            default:

                throw new IllegalArgumentException("unexpected viewType: " + viewType);

        }

    }


    @Override

    public int getItemCount(){return myArrayList.size();}


    @NonNull

    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        int layout;

        RecyclerView.ViewHolder viewHolder;

        switch (viewType){

            case TYPE_TABLE:

                layout = R.layout.cardview_tableview_withexpandability;

                View tblView = LayoutInflater

                        .from(parent.getContext())

                        .inflate(layout, parent, false);

                viewHolder = new TblViewHolder(tblView);

                break;

            case TYPE_SEPARATOR:

                layout = R.layout.lineseparatorforrecyclerview;

                View separatorView = LayoutInflater

                        .from(parent.getContext())

                        .inflate(layout, parent, false);

                viewHolder = new SeparatorViewHolder(separatorView);

                break;

            default:

                throw new IllegalArgumentException("unexpected viewType: " + viewType);

        }

        return viewHolder;

    }


    public class TblViewHolder extends RecyclerView.ViewHolder {

        final Typeface iconFont = FontManager.getTypeface(context, FontManager.FONTAWESOME);


        private Button btnToday, btnYesterday, btnThisWeek, btnThisMonth;

        private TextView arrowexpandcollapseTextView, sectionNameTextView;


        TblViewHolder(View itemView) {

            super(itemView);


            btnMale.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).create();


                    alertDialog.setButton(Dialog.BUTTON_NEUTRAL,"OK",new DialogInterface.OnClickListener(){

                        @Override

                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                        }

                    });



                    LayoutInflater inflater = LayoutInflater.from(context);

                    @SuppressLint("InflateParams") View content = inflater.inflate(R.layout.dialog_stats, null);

                    alertDialog.setView(content);


                    //Pulling String out of bundle

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

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

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

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



                    ImageView imgdialogMain = content.findViewById(R.id.imgView_genderA);

                    ivGenderA.setImageResource(R.drawable.ic_male);


                    TextView tvGoalReached = content.findViewById(R.id.txtView_dialog_goalreached);

                    tvGoalReached.setText(goalreached);



                    TextView tvDay = content.findViewById(R.id.txtView_day);

                    tvDay.setText(day);


                    TextView tvImgviewWalking = content.findViewById(R.id.imgView_walking);



                    TextView tvDistance = content.findViewById(R.id.txtView_distance);

                    tvDistance.setText(distance);


                    TextView tvImgviewFire = content.findViewById(R.id.imgView_fire);


                    TextView tvCaloriesBurned = content.findViewById(R.id.txtView_location);

                    tvCaloriesBurned.setText(calories);


                    alertDialog.show();

                }

            });

        }

    }

}

目前,您只是在片段中創(chuàng)建一個新包并向其中添加字符串,然后什么也不做,然后在適配器中創(chuàng)建另一個新包并嘗試拉出不存在的字符串。


要更新數(shù)據(jù),您可以在需要更新數(shù)據(jù)時在要調(diào)用的適配器中嘗試類似的操作(不過,我不確定 notifyDataSetChanged() 是否可以在適配器內(nèi)部工作):


//Update Bundle

public void updateView(Bundle bundle){

    mBundle = bundle;

    this.notifyDataSetChanged();

}


查看完整回答
反對 回復(fù) 2022-01-06
?
弒天下

TA貢獻1818條經(jīng)驗 獲得超8個贊

您正在嘗試從這里的新捆綁包中獲取額外內(nèi)容:


Bundle bundle = new Bundle();

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

...

您需要獲取存儲該信息的包。捆綁包只是信息的“容器”,因此您需要存儲它然后檢索它。


您永遠不會將捆綁包保存在任何地方,因此您以后無法閱讀。


我不確定你想在這里做什么,所以我不能給你一個例子,但看看這個:


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));

您正在創(chuàng)建該捆綁包,然后存儲信息,僅此而已。你永遠不會把它存放在任何地方。


捆綁包的基本用法是將其存儲在意圖上,然后檢索該意圖并獲取捆綁包。


查看完整回答
反對 回復(fù) 2022-01-06
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號