10

Hy, i've got list view in my fragment. I am replacing this listview fragment with my other fragment but when i want back to my list, it's empty. I thought i could restore my list items but i don't know how. I Assumed that accord with Fragment lifecycle my adapter will be reusable but looks it's not. Here is my code:

public class ThreadListFragment extends Fragment implements FragmentConnectionStatus, ListFragment, OnClickListener, OnItemClickListener{
       private ListView ListView;
       private PilotController Activity;
       private Button ButtonStartNewThread;
       private boolean Paused;
       private ThreadInfoAdapter adapter;

@Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
   * Inflate the layout for this fragment
   */
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.threads_list_fragment, container, false);
        System.out.println(this.Paused);
        if(this.Paused == false){
            this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
            ArrayList<Guid> strs = new ArrayList<Guid>();
            adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
        }
        this.ListView.setAdapter(adapter);
        this.ListView.setItemsCanFocus(true);
        this.ListView.setOnItemClickListener(this);
        this.Activity = (PilotController) getActivity();
        this.ButtonStartNewThread = (Button) view.findViewById(R.id.buttonStartThread);
        this.ButtonStartNewThread.setOnClickListener(this);
        return view;
   }

@Override
public void onPause()
{
    super.onPause();
    this.Paused = true;
}

but when i return to my old list, it's empty. Could you give me some advice?

SOLUTION

I found a solution. ListView is probably removing from 'draw stack' so when calling OnDeleteView your old list is unusable next time although reference to this object is still set. The solution is to recreate ListView and set old adapter. This adapter due to this tutorial http://developer.android.com/guide/components/fragments.html will survive but view is recreated (so old listview doesn't exist anymore (it exist but it's not drawable). I had to change this lines:

    if(this.Paused == false){
        this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
        ArrayList<Guid> strs = new ArrayList<Guid>();
        adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
    }

to:

    this.ListView = (ListView) view.findViewById(R.id.listViewActiveThreads);
    if(this.Paused == false){
        ArrayList<Guid> strs = new ArrayList<Guid>();
        adapter = new ThreadInfoAdapter(view.getContext(), strs, this);
    }
Puchacz
  • 1,987
  • 2
  • 24
  • 38

2 Answers2

0

In "onActivityCreated" you can just check if your dataList (in your case "strs) is empty. If it is not then just use it, otherwise create a new one. In code it would look like this:

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (strs == null)
        strs = new ArrayList<Guid>();

    mListView = (ListView)getActivity().findViewById(R.id.listViewActiveThreads);

    mPersonAdapter = new ArrayAdapterPerson(getActivity(),mPersonList,this);
    mListView.setAdapter(mPersonAdapter);

}
user3466562
  • 465
  • 2
  • 10
  • 23
0

I found my solution here: ListFragment is empty after going back

use transaction.hide(this):

ft.addToBackStack(null);
        ft.hide(this);
        ft.add(R.id.frag_frame, lyrics);
        ft.commit();
Community
  • 1
  • 1
bat-el.g
  • 309
  • 4
  • 6