简体   繁体   English

将自定义ArrayAdapter保存在Fragment的savedInstanceState中

[英]Save Custom ArrayAdapter in savedInstanceState in Fragment

I have been working in a App which use the Evernote API, it´s already working, but right now I´m including the savedInstanceStates in the different activities. 我一直在使用Evernote API的应用程序中工作,但现在已经可以使用,但是现在我在不同的活动中包括了savedInstanceStates。 All of them are working least one. 他们全部都至少在工作。 When I show my notes from Evernote in a List and I, for example, rotate the screen, the list view is created again. 例如,当我在列表中显示Evernote中的笔记并旋转屏幕时,将再次创建列表视图。 I want to save the Instance of it. 我要保存它的实例。

I already tried to use setRetainInstance(true) but when the screen is rotate the Listview is like freeze, I can´t tab in it or scroll it. 我已经尝试过使用setRetainInstance(true),但是当屏幕旋转时,Listview就像冻结一样,我无法在其中进行制表或滚动。 I was reading in other post and it´s because I add the interaction with a set.OnClickListener which is called in the MainActivity, and no in this Fragment. 我在另一篇文章中读到,这是因为我添加了一个与set.OnClickListener的交互,该交互在MainActivity中被调用,而在此Fragment中没有。

So, I´m looking for a way to store the savedInstance of the array to call it when I rotate the screen or inResume() etc. 因此,我正在寻找一种方法来存储数组的savedInstance,以便在旋转屏幕或inResume()等时调用它。

This is my code (I just add the related method because MainActivity is so long): 这是我的代码(因为MainActivity很长,所以我只添加了相关方法):

public class MainActivity extends AppCompatActivity implements  NoteListFragment.OnFragmentInteractionListener,EvernoteLoginFragment.ResultCallback  {    
private EvernoteSession mEvernoteSession;    
private static final EvernoteSession.EvernoteService EVERNOTE_SERVICE = EvernoteSession.EvernoteService.SANDBOX;   
private NoteFilter filter = new NoteFilter();    
private EvernoteNoteStoreClient noteStoreClient;   
private static NoteListFragment noteFragment;    
private DrawerLayout mDrawerLayout;
private int mSelectedNavItem;
private static final String KEY_SELECTED_NAV_ITEM = "KEY_SELECTED_NAV_ITEM";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    noteFragment = (NoteListFragment) getFragmentManager().findFragmentById(R.id.note_list);

    if (noteFragment == null) {
        noteFragment = new NoteListFragment();
        getFragmentManager()
                .beginTransaction()
                .add(R.id.main_content, noteFragment)
                .commit();
    }

    if (savedInstanceState != null) {
        mSelectedNavItem = savedInstanceState.getInt(KEY_SELECTED_NAV_ITEM, mSelectedNavItem);
    } else {
        mSelectedNavItem = R.id.nav_item_update;
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (!isTaskRoot()) {

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer);

    mDrawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            onNavDrawerItemClick(menuItem.getItemId());
            return true;
        }
    });
    navigationView.getMenu().findItem(mSelectedNavItem).setChecked(true);

    loginEvernote();

    if (!mEvernoteSession.isLoggedIn()) {
        mEvernoteSession.authenticate(this);
        // finish();
        return;
    } else {
       //this is comment because when it will be save correctly I don't need to call all times loadNotes
       // if (savedInstanceState == null) {
            loadNotes(filter);
       // }

    }

}  protected void loadNotes(NoteFilter filter) {


    noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();

    noteStoreClient.findNotesAsync(filter, 0, 100, new EvernoteCallback<NoteList>() {


        @Override
        public void onSuccess(NoteList result) {

            noteFragment.setNotes(result.getNotes());

            Toast.makeText(getApplicationContext(), R.string.onSucessLoadToast, Toast.LENGTH_LONG).show();
            Log.d(getClass().getSimpleName(), "notes: " + result);
        }

        @Override

        public void onException(Exception exception) {
            Log.e(getClass().getSimpleName(), "Error al recuperar las notas", exception);
        }
    });
}

And now the Fragment: 现在是片段:

public class NoteListFragment extends Fragment implements AdapterView.OnItemClickListener {

private OnFragmentInteractionListener mListener;
private ListAdapter mAdapter;
private AbsListView mListView;
private List<Note> Notes;
private EvernoteSession mEvernoteSession;
private ArrayAdapter<NoteWrapped> adapter;
FloatingActionButton newNoteButton;

public NoteListFragment() {
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = new ArrayAdapter<NoteWrapped>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1);
    //setRetainInstance(true);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_note_list, container, false);


    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
    mListView.setOnItemClickListener(this);
    newNoteButton = (FloatingActionButton) view.findViewById(R.id.fab);
    newNoteButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            FragmentManager frmg = getFragmentManager();
            frmg.beginTransaction()
                    .replace(R.id.main_content, new CreateNoteFragment())
                    .addToBackStack(null)
                    .commit();
        }
    });

    return view;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (null != mListener) {


        try {
            mListener.onFragmentInteraction(Notes.get(position));
        } catch (EDAMUserException e) {
            e.printStackTrace();
        } catch (EDAMSystemException e) {
            e.printStackTrace();
        } catch (EDAMNotFoundException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onResume(Bundle savedInstanceState);
    public void onFragmentInteraction(Note note) throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException, ExecutionException, InterruptedException;
}

public class NoteWrapped{

    private final String title;

    public NoteWrapped(Note note) {
        this.title = note.getTitle();
    }

    @Override
    public String toString() {
        return title;
    }
}

public void setNotes(List<Note> notes) {
    this.Notes = notes;

    adapter = (ArrayAdapter<NoteWrapped>) mAdapter;

    adapter.clear();

    for (Note note : notes) {
        adapter.add(new NoteWrapped(note));
    }
}
}

Just in case, the .xml: 以防万一,.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#E8F5E9"
    android:fitsSystemWindows="false"
    app:itemTextColor="@android:color/secondary_text_light"
    app:menu="@menu/settings_header">

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Already fixed. 已经修复。 I found the answer in other post , I just adapted for use it in Fragment. 我在其他帖子中找到了答案,我只适合在Fragment中使用它。

Something like this: 像这样:

 @Override public Object onRetainNonConfigurationInstance() { return this.m_adapter.getItems(); } And then in your onCreate(): @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // more init stuff here sResultsArr = (ArrayList<SearchItems>)getLastNonConfigurationInstance(); if(sResultArr == null) { sResultsArray = new ArrayList<SearchItems>(); // or some other initialization } self.m_adapter = new ResultsAdapter(home.this, R.layout.listrow, sResultsArr,home.this); // ... } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM