我有片段(在FragmentStatePagerAdapter中)具有全局變量,而在onOptionsItemSelected中我引用此值。它第一次正常工作,當我回到同一頁面並更新這個全局變量時,現在如果我引用onOptionsItemSelected中的值,它保存了以前的值而不是當前的值。 我試圖在onOptionsItemSelected中打印「this」引用,它指的是第一個引用。而其他方法中的值指的是更新的值。onOptionsItemSelected在FragmentStatePagerAdapter中的片段保存了此前的參考
編輯1:添加的代碼片斷
public class CustomizeFieldFragment extends BaseFragment implements CustomizeFieldListAdapter.IOnSelectCustomizeField {
private MenuItem mMenuItem;
private List<String> mSelectedFieldsId;
private boolean mShowDoneMenu;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.customize_field_done, menu);
mMenuItem = menu.findItem(R.id.action_done);
mMenuItem.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
updateSelectedFields();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onSelect(List<String> selectedFieldsId) {
if(!selectedFieldsId.isEmpty()){
mSelectedFieldsId = new ArrayList<>(selectedFieldsId);
mShowDoneMenu = true;
}
}
private void updateSelectedFields() {
String[] selectionArgs = mSelectedFieldsId.toArray(new String[mSelectedFieldsId.size()]);
Uri uri = Contract.DefectFieldEntry.buildDefectFieldUri(baseUrl, domainName, projectName);
ContentValues cv = new ContentValues();
cv.put(Contract.DefectFieldEntry.COLUMN_IS_FIELD_SELECTED, true);
// mContext.getContentResolver().update(uri, cv, null, selectionArgs);
}
}
的問題是與 「mSelectedFieldsId」 變量。當我在onOptionsItemSelected中引用它時,它指的是存儲的舊值,而在其他方法中它指的是實際值。
編輯2:適配器代碼片段
public class CustomizeFieldListAdapter extends RecyclerView.Adapter<CustomizeFieldListAdapter.CustomizeFieldListViewHolder> {
private static final String TAG = CustomizeFieldListAdapter.class.getSimpleName();
private Cursor mCursor;
private SparseBooleanArray mSelectedItems;
private IOnSelectCustomizeField onSelectCustomizeField;
private List<String> mSelectedItemsId;
public CustomizeFieldListAdapter(IOnSelectCustomizeField iOnSelectCustomizeField){
onSelectCustomizeField = iOnSelectCustomizeField;
mSelectedItems = new SparseBooleanArray();
mSelectedItemsId = new ArrayList<>();
}
public class CustomizeFieldListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvFieldName;
ImageView selectionIcon;
public CustomizeFieldListViewHolder(View itemView) {
super(itemView);
tvFieldName = (TextView) itemView.findViewById(R.id.customize_field_name);
selectionIcon = (ImageView) itemView.findViewById(R.id.selection_icon);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int adapterPos = getAdapterPosition();
toggleSelection(adapterPos);
}
}
@Override
public CustomizeFieldListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_customize_field,
parent, false);
return new CustomizeFieldListViewHolder(rootView);
}
@Override
public void onBindViewHolder(CustomizeFieldListViewHolder holder, int position) {
mCursor.moveToPosition(position);
holder.tvFieldName.setText(mCursor.getString(1));
if(mSelectedItems.get(position, false))
holder.selectionIcon.setImageResource(R.mipmap.select);
else
holder.selectionIcon.setImageResource(R.mipmap.selection_icon);
}
@Override
public int getItemCount() {
return mCursor == null ? 0 : mCursor.getCount();
}
public void swapCursor(Cursor cursor){
mCursor = cursor;
notifyDataSetChanged();
}
private void toggleSelection(int position){
mCursor.moveToPosition(position);
if(mSelectedItems.get(position, false)){
mSelectedItems.delete(position);
mSelectedItemsId.remove(mCursor.getString(0));
}
else{
mSelectedItems.put(position, true);
mSelectedItemsId.add(mCursor.getString(0));
}
onSelectCustomizeField.onSelect(mSelectedItemsId);
notifyItemChanged(position);
}
public interface IOnSelectCustomizeField {
void onSelect(List<String> selectedItemsId);
}
}
添加代碼片段。 –
如果您發佈代碼更新變量的代碼片段,它會更容易幫助。 –
你還可以將代碼從適配器傳遞到'Fragment'嗎? –