[英]Data in RecyclerView not being passed from Fragment to Adapter
創建RecyclerView
,我注意到在Bundle
中聲明的字符串沒有從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();
}
private void initRVAdapter(){
List<Object> itemsList = new ArrayList<>();
itemsList.add(new RVLineSeparator());
itemsList.add(new RVTable("Stats", "Today", "Yesterday", "This week", "This month"));
RVItemsAapter itemsListAdapter = new RVItemsAapter(getContext());
mRecyclerView.setAdapter(itemsListAdapter);
itemsListAdapter.setCallSMSFeed(itemsList);
itemsListAdapter.notifyDataSetChanged();
}
}
適配器類
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;
public RVItemsAdapter(Context context){
this. context=context;
}
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");
}
@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);
Bundle bundle = getBundle();
String goalreached = bundle.getString("goalreached");
String day = bundle.getString("day");
String distance = bundle.getString("distance");
String calories = bundle.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();
}
});
}
}
}
您需要將第一個創建的捆綁軟件傳遞到適配器中(可能是在創建適配器時)
這是帶有必要更改的課程
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();
}
您正在嘗試從此處的新捆綁包中獲取其他服務:
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));
您正在創建該捆綁包,然后存儲信息,僅此而已。 您永遠不會將其存儲在任何地方。
捆綁軟件的基本用法是將其存儲在Intent上,然后檢索該Intent並獲取捆綁軟件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.