2 回答
TA贡献1829条经验 获得超6个赞
您需要将第一个创建的包传递到您的适配器中(可能是在创建时)
这是具有必要更改的类
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();
}
});
}
}
}
目前,您只是在片段中创建一个新包并向其中添加字符串,然后什么也不做,然后在适配器中创建另一个新包并尝试拉出不存在的字符串。
要更新数据,您可以在需要更新数据时在要调用的适配器中尝试类似的操作(不过,我不确定 notifyDataSetChanged() 是否可以在适配器内部工作):
//Update Bundle
public void updateView(Bundle bundle){
mBundle = bundle;
this.notifyDataSetChanged();
}
TA贡献1818条经验 获得超8个赞
您正在尝试从这里的新捆绑包中获取额外内容:
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));
您正在创建该捆绑包,然后存储信息,仅此而已。你永远不会把它存放在任何地方。
捆绑包的基本用法是将其存储在意图上,然后检索该意图并获取捆绑包。
添加回答
举报