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

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)建該捆綁包,然后存儲信息,僅此而已。你永遠不會把它存放在任何地方。
捆綁包的基本用法是將其存儲在意圖上,然后檢索該意圖并獲取捆綁包。
添加回答
舉報