user1800825
user1800825

Reputation: 203

Replace ListFragment

I am using a ListFragment for displaying a list from a database in my activity. I have included a search function. Unfortunately the "old" ListFragments seem to remain in the background and the ListFragments containing the result of the query are displayed on top of it. How can I avoid, that the old ListFragments are displayed?

My FragmentActivity:

private Button buttonSearch; 
    private TextView searchString; 
    public static String search = null; 
    static IShoppinglist shoppinglistManager;
    static IAktionen aktionenManager; 
    private AktionenListListFragment listFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "ListFragmentActivity created"); 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articlelist);

        shoppinglistManager = new Shoppinglist(this);
        aktionenManager = new Aktionen(this); 

        buttonSearch = (Button) findViewById(R.id.search_Button);
        buttonSearch.setOnClickListener(searchListAktionen);

        //show all entries on start
        listFragment = new AktionenListListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();
    }   

    OnClickListener searchListAktionen = new OnClickListener() {
        public void onClick(View v) {


            try{
                searchString = (TextView) findViewById(R.id.input_search_bezeichnung); 
                search = searchString.getText().toString().trim();
                Log.d(TAG, "search Button clicked "+search); 

                if(search.trim().length()==0){
                    search=null;
                }

                //show all entries on start
                listFragment = new AktionenListListFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment).commit();

            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    };

Thanks in advance,

update:

thank you for your answers. I tried to implement them, but the main problem seems to be nowthat the onCreate and onActivityCreated method in the ListFragment are called twice (as I can see in my log messages).

my new code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "ListFragmentActivity created"); 
        super.onCreate(savedInstanceState);

        //force commit
        getSupportFragmentManager().executePendingTransactions();


        if(getSupportFragmentManager().findFragmentByTag(tag) == null) {
            setContentView(R.layout.articlelist);

            shoppinglistManager = new Shoppinglist(this);
            aktionenManager = new Aktionen(this); 

            buttonSearch = (Button) findViewById(R.id.search_Button);
            buttonSearch.setOnClickListener(searchListAktionen);
            listFragment = new AktionenListListFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_articlelist, listFragment,tag).commit();
        }else{
            Log.d(TAG, "ListFragment already exists"); 
        }
    }

I tried to set a unique tag for my ListFragment but this solution does not work.

I guess that one of the ListFragments is displayed in the background and the other is updated.

Upvotes: 0

Views: 1556

Answers (1)

dumamilk
dumamilk

Reputation: 2153

So first you need to stop making new ListFragments everytime your list is refreshed and just have a public method in your ListFragment that the Activity can call to restart the loader with the proper parameters. Then:

In your onLoadFinished(),

you should make a new adapter with the list you want to replace it with

myAdapter = new AktionenListCustomCursorAdapter(getActivity(), myCursor);

and call:

this.getListView().setAdapter(myAdapter);

So:

    public void onLoadFinished(Loader<Cursor> mAdapter, Cursor myCursor) {
                if(myCursor!=null){
                    //getting the data from the database
                    Log.d(TAG, "search String "+AktionenListFragmentActivity.search);
                    if(AktionenListFragmentActivity.search==null){
                        myCursor = AktionenListFragmentActivity.aktionenManager.fetchAllArticles();
                    }else{
                        myCursor = AktionenListFragmentActivity.aktionenManager.fetchItemsByBezeichnung(AktionenListFragmentActivity.search);
                    }
myAdapter = new AktionenListCustomCursorAdapter(getActivity(), myCursor);
                    this.getListView().setAdapter(myAdapter);
                }
            }

Hopefully this solved your question as I understood it. If you have any questions please leave it in the comment below and I will expand my answer. If it worked for you please accept answer. :D

Upvotes: 2

Related Questions