0

我正在使用 ListFragment 在我的活动中显示来自数据库的列表。我已经包括了一个搜索功能。不幸的是,“旧” ListFragments 似乎保留在后台,并且包含查询结果的 ListFragments 显示在其顶部。如何避免显示旧的 ListFragments?

我的片段活动:

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();
            }
        }
    };

提前致谢,

更新:

谢谢您的回答。我试图实现它们,但主要问题似乎是现在 ListFragment 中的 onCreate 和 onActivityCreated 方法被调用了两次(正如我在日志消息中看到的那样)。

我的新代码:

@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"); 
        }
    }

我试图为我的 ListFragment 设置一个唯一标签,但这个解决方案不起作用。

我猜其中一个 ListFragments 显示在后台,另一个已更新。

4

1 回答 1

2

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

于 2013-02-13T19:41:50.230 回答