当我第一次启动我的应用程序并且用户点击recyclerview中的项目时,所有内容都按预期进行初始化。
当用户点击后退按钮返回主项目列表页面然后点击相同或其他项目时,页面不会加载相同的内容。
我的初始布局与没有更新位置名称和坐标的第二张图片相同。
令我感到沮丧的是,日期名称是以相同的方法初始化的。我已经在很多方面对它进行了调试,发现代码一定不能运行 - 它内部的代码在重新加载时对项目详细信息页面的结果没有任何影响。此代码仅在第一次运行时影响实际布局,并且不会影响任何其他时间。
RecyclerView适配器:
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<LocationContent.LocationItem> mValues;
public SimpleItemRecyclerViewAdapter(List<LocationContent.LocationItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public LocationContent.LocationItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
应该运行的代码:
Log.i("LOAD DATA", "Called");
Log.e("SWITCH CASE (IDF)", AssetLoader.getMondayName(mItem.id));
// To layout below the textView to avoid crashes
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.textView);
switch(AssetLoader.getLastItemDayName(mItem.id)) {
case "Sun":
sunday.setLayoutParams(p);
break;
case "Mon":
monday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "Tues":
tuesday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "Wed":
wednesday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "Thurs":
thursday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "Fri":
friday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "Sat":
saturday.setLayoutParams(p);
// Make sunday layout before monday (as its default is below textView)
p.removeRule(RelativeLayout.BELOW);
p.addRule(RelativeLayout.BELOW, R.id.monday);
sunday.setLayoutParams(p);
break;
case "":
sunday.setLayoutParams(p);
break;
}
Log.e("MONDAY NAME EDIT()", AssetLoader.getMondayName(mItem.id));
if (!AssetLoader.dataInMonday(mItem.id)/*AssetLoader.getMondayName(mItem.id).equals("") || AssetLoader.getMondayName(mItem.id).equals("Monday:")*/) {
monday.setVisibility(View.GONE);
monday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsMonday(mItem.id, "");
} else {
monday.setVisibility(View.VISIBLE);
monday_detail.setVisibility(View.VISIBLE);
monday.setText(AssetLoader.getMondayName(mItem.id));
if(AssetLoader.getItemDetailsMonday(mItem.id).equals("")) {
monday_detail.setText(R.string.no_data);
} else {
monday_detail.setText(AssetLoader.getItemDetailsMonday(mItem.id));
}
}
if (!AssetLoader.dataInTuesday(mItem.id)/*AssetLoader.getTuesdayName(mItem.id).equals("") || AssetLoader.getTuesdayName(mItem.id).equals("Tuesday:")*/) {
tuesday.setVisibility(View.GONE);
tuesday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsTuesday(mItem.id, "");
} else {
tuesday.setVisibility(View.VISIBLE);
tuesday_detail.setVisibility(View.VISIBLE);
tuesday.setText(AssetLoader.getTuesdayName(mItem.id));
if(AssetLoader.getItemDetailsMonday(mItem.id).equals("")) {
tuesday_detail.setText(R.string.no_data);
} else {
tuesday_detail.setText(AssetLoader.getItemDetailsTuesday(mItem.id));
}
}
if (!AssetLoader.dataInWednesday(mItem.id)/*AssetLoader.getWednesdayName(mItem.id).equals("") || AssetLoader.getWednesdayName(mItem.id).equals("Wednesday:")*/) {
wednesday.setVisibility(View.GONE);
wednesday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsWednesday(mItem.id, "");
} else {
wednesday.setVisibility(View.VISIBLE);
wednesday_detail.setVisibility(View.VISIBLE);
wednesday.setText(AssetLoader.getWednesdayName(mItem.id));
if(AssetLoader.getItemDetailsWednesday(mItem.id).equals("")) {
wednesday_detail.setText(R.string.no_data);
} else {
wednesday_detail.setText(AssetLoader.getItemDetailsWednesday(mItem.id));
}
}
if (!AssetLoader.dataInThursday(mItem.id)/*AssetLoader.getThursdayName(mItem.id).equals("") || AssetLoader.getThursdayName(mItem.id).equals("Thursday:")*/) {
thursday.setVisibility(View.GONE);
thursday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsThursday(mItem.id, "");
} else {
thursday.setVisibility(View.VISIBLE);
thursday_detail.setVisibility(View.VISIBLE);
thursday.setText(AssetLoader.getThursdayName(mItem.id));
if(AssetLoader.getItemDetailsThursday(mItem.id).equals("")) {
thursday_detail.setText(R.string.no_data);
} else {
thursday_detail.setText(AssetLoader.getItemDetailsThursday(mItem.id));
}
}
if (!AssetLoader.dataInFriday(mItem.id)/*AssetLoader.getFridayName(mItem.id).equals("") || AssetLoader.getFridayName(mItem.id).equals("Friday:")*/) {
friday.setVisibility(View.GONE);
friday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsFriday(mItem.id, "");
} else {
friday.setVisibility(View.VISIBLE);
friday_detail.setVisibility(View.VISIBLE);
friday.setText(AssetLoader.getFridayName(mItem.id));
if(AssetLoader.getItemDetailsFriday(mItem.id).equals("")) {
friday_detail.setText(R.string.no_data);
} else {
friday_detail.setText(AssetLoader.getItemDetailsFriday(mItem.id));
}
}
if (!AssetLoader.dataInSaturday(mItem.id)/*AssetLoader.getSaturdayName(mItem.id).equals("") || AssetLoader.getSaturdayName(mItem.id).equals("Saturday:")*/) {
saturday.setVisibility(View.GONE);
saturday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsSaturday(mItem.id, "");
} else {
saturday.setVisibility(View.VISIBLE);
saturday_detail.setVisibility(View.VISIBLE);
saturday.setText(AssetLoader.getSaturdayName(mItem.id));
if(AssetLoader.getItemDetailsSaturday(mItem.id).equals("")) {
saturday_detail.setText(R.string.no_data);
} else {
saturday_detail.setText(AssetLoader.getItemDetailsSaturday(mItem.id));
}
}
if (!AssetLoader.dataInSunday(mItem.id)/*AssetLoader.getSundayName(mItem.id).equals("") || AssetLoader.getSundayName(mItem.id).equals("Sunday:")*/) {
sunday.setVisibility(View.GONE);
sunday_detail.setVisibility(View.GONE);
AssetLoader.setItemDetailsSunday(mItem.id, "");
} else {
sunday.setVisibility(View.VISIBLE);
sunday_detail.setVisibility(View.VISIBLE);
sunday.setText(AssetLoader.getSundayName(mItem.id));
if(AssetLoader.getItemDetailsSunday(mItem.id).equals("")) {
sunday_detail.setText(R.string.no_data);
} else {
sunday_detail.setText(AssetLoader.getItemDetailsSunday(mItem.id));
}
}
请注意,当运行应用程序时,Log.e和Log.i调用甚至不会显示在logcat / Android Monitor中。当用户界面的其余部分被呼叫时,该代码正在被呼叫!感谢您提供的任何信息!
答案 0 :(得分:0)
在onResume方法中为RecyclerView设置适配器。
答案 1 :(得分:0)
我找到了问题。当我的Item的DetailView第一次加载时,我的星期一,星期二等变量为null,因此它们被设置为当前布局上的TextViews。按下后退按钮时,这些变量不再为空,但仍然指向先前布局的变量。
代码不正确:
if(monday == null) {
monday = (TextView) rootView.findViewById(R.id.monday);
tuesday = (TextView) rootView.findViewById(R.id.tuesday);
wednesday = (TextView) rootView.findViewById(R.id.wednesday);
thursday = (TextView) rootView.findViewById(R.id.thursday);
friday = (TextView) rootView.findViewById(R.id.friday);
saturday = (TextView) rootView.findViewById(R.id.saturday);
sunday = (TextView) rootView.findViewById(R.id.sunday);
}
if(monday_detail == null) {
monday_detail = (TextView) rootView.findViewById(R.id.monday_detail);
tuesday_detail = (TextView) rootView.findViewById(R.id.tuesday_detail);
wednesday_detail = (TextView) rootView.findViewById(R.id.wednesday_detail);
thursday_detail = (TextView) rootView.findViewById(R.id.thursday_detail);
friday_detail = (TextView) rootView.findViewById(R.id.friday_detail);
saturday_detail = (TextView) rootView.findViewById(R.id.saturday_detail);
sunday_detail = (TextView) rootView.findViewById(R.id.sunday_detail);
}
正确代码(没有空检查):
monday = (TextView) rootView.findViewById(R.id.monday);
tuesday = (TextView) rootView.findViewById(R.id.tuesday);
wednesday = (TextView) rootView.findViewById(R.id.wednesday);
thursday = (TextView) rootView.findViewById(R.id.thursday);
friday = (TextView) rootView.findViewById(R.id.friday);
saturday = (TextView) rootView.findViewById(R.id.saturday);
sunday = (TextView) rootView.findViewById(R.id.sunday);
monday_detail = (TextView) rootView.findViewById(R.id.monday_detail);
tuesday_detail = (TextView) rootView.findViewById(R.id.tuesday_detail);
wednesday_detail = (TextView) rootView.findViewById(R.id.wednesday_detail);
thursday_detail = (TextView) rootView.findViewById(R.id.thursday_detail);
friday_detail = (TextView) rootView.findViewById(R.id.friday_detail);
saturday_detail = (TextView) rootView.findViewById(R.id.saturday_detail);
sunday_detail = (TextView) rootView.findViewById(R.id.sunday_detail);
因此,通过删除空检查,我的代码每次都被正确初始化并且没有错误。