来自游标加载器的ListFragment无法显示有关方向更改的任何内容

时间:2012-07-25 16:52:46

标签: android android-listfragment android-cursorloader commonsware-cwac

我正在开发一个使用ActionBarSherlock和ViewPagerIndicator构建的Android应用程序,其minSDK为8(Android 2.2),targetSDK为16(Android 4.1),使用Android兼容性库。我有一个应用程序的消息传递部分,其中消息存储在SQLite数据库中,我使用CommonsWare的cwac-loaderex来使用SQLite数据库的游标加载器。当活动加载时,一切都运行良好并显示消息,但是当我旋转设备时,它只显示带有加载cicle的ListFragment。

启用LoaderManager.enableDebugLogging(true)当我旋转时重用现有的加载器,我也在onDestroy中尝试了getSupportLoaderManager()。destroyLoader(ID)并且正在创建新的加载器但我仍然有相同的结果,一个ListFragment加载圈。我在打开消息传递活动之前已经在两个方向进行了测试,并且它们都工作正常,当我在消息传递活动可见时轮换时,这只是一个问题。我还尝试以纵向方式进入消息传递活动,单击一条消息,将我带到另一个活动而不破坏消息传递活动,更改为格局,然后按回消息传递活动,一切正常。

我无法弄清楚为什么在旋转后没有显示消息。我会非常感谢任何想法或帮助。我在下面列出了我的消息传递活动。

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.commonsware.cwac.loaderex.acl.SQLiteCursorLoader;
import com.viewpagerindicator.TitlePageIndicator;

import com.lukekorth.DB.DatabaseHelper;
import com.lukekorth.Helpers.MessageFragment;

public class MyMessagesActivity extends SherlockFragmentActivity implements
LoaderManager.LoaderCallbacks<Cursor> {

        private FragmentAdapter mFragmentAdapter;
    private ViewPager mPager;
    private DatabaseHelper mDB;
    private SimpleCursorAdapter mInboxCursorAdapter;
    private SimpleCursorAdapter mArchiveCursorAdapter;

    private MessageFragment[] content = new MessageFragment[2];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messages);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSherlock().getActionBar().setDisplayHomeAsUpEnabled(true);

        mDB = new DatabaseHelper(this);

        content[0] = new MessageFragment();
        content[1] = new MessageFragment();

        /////////--------------------///////
        LoaderManager.enableDebugLogging(true);

        mInboxCursorAdapter = new SimpleCursorAdapter(this, R.layout.message_item, null, new String[] { DatabaseHelper.senderName,
                DatabaseHelper.lastUpdate, DatabaseHelper.subject, DatabaseHelper.lastSnippet }, new int[] { R.id.name, R.id.date,
                R.id.subject, R.id.snippet });

        mArchiveCursorAdapter = new SimpleCursorAdapter(this, R.layout.message_item, null, new String[] { DatabaseHelper.senderName,
                DatabaseHelper.lastUpdate, DatabaseHelper.subject, DatabaseHelper.lastSnippet }, new int[] { R.id.name, R.id.date,
                R.id.subject, R.id.snippet });

        content[0].setListAdapter(mInboxCursorAdapter);
        content[1].setListAdapter(mArchiveCursorAdapter);

        getSupportLoaderManager().initLoader(0, null, this);
        getSupportLoaderManager().initLoader(1, null, this);
        ///////////////------------------////////////

        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        mFragmentAdapter.updateContent(content);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mFragmentAdapter);

        TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(mPager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        getSupportLoaderManager().destroyLoader(0);
        getSupportLoaderManager().destroyLoader(1);

        mDB.close();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int folder, Bundle arg1) {
        String query = "select " + DatabaseHelper.threads + "." + DatabaseHelper.threadKey + "," + DatabaseHelper.subject +
                "," + DatabaseHelper.archive + "," + DatabaseHelper.otherUser + "," + DatabaseHelper.lastUpdate + "," +
                DatabaseHelper.lastSnippet + "," + DatabaseHelper.senderPicture + "," + DatabaseHelper.senderName + " from " +
                DatabaseHelper.threads + " left join " + DatabaseHelper.senders + " on " + DatabaseHelper.threads + "." +
                DatabaseHelper.otherUser + " = " + DatabaseHelper.senders + "." + DatabaseHelper.senderKey + " where " +
                DatabaseHelper.archive;

        if(folder == 0)
            query += " = 0 order by ";
        else
            query += " = 1 order by ";

        query += DatabaseHelper.lastUpdate + " desc";

        return new SQLiteCursorLoader(this, mDB, query, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if(loader.getId() == 0)
            mInboxCursorAdapter.swapCursor(cursor);
        else
            mArchiveCursorAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        if(loader.getId() == 0)
            mInboxCursorAdapter.swapCursor(null);
        else
            mArchiveCursorAdapter.swapCursor(null);
    }
}

class FragmentAdapter extends FragmentPagerAdapter {
    protected static final String[] TITLE = new String[] { "Inbox", "Archive", };

    private ListFragment[] mContent;

    private int mCount = TITLE.length;

    public FragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    public void updateContent(MessageFragment[] content){
        mContent = content;
    }

    @Override
    public Fragment getItem(int position) {
        return mContent[position % mContent.length];
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return FragmentAdapter.TITLE[position % TITLE.length];
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明,ViewPager和片段标签存在一些奇怪之处。我的解决方案是在onSaveInstanceState()中的事务中添加片段,然后在onCreate()(第二段代码)中添加片段,以使用ViewPager中生成的标记通过标记获取片段。我能够找出this question中使用的标签方案。希望这可以节省一些人工作。

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.add(content[0], content[0].getTag());
    ft.add(content[1], content[1].getTag());

    ft.commit();
}




    FragmentManager fm = getSupportFragmentManager();

    content[0] = (MessageFragment) fm.findFragmentByTag("android:switcher:"+ R.id.pager + ":0");
    content[1] = (MessageFragment) fm.findFragmentByTag("android:switcher:"+ R.id.pager + ":1");

    if(content[0] == null || content[1] == null){
        content[0] = new MessageFragment();
        content[1] = new MessageFragment();

        content[0].setListAdapter(mInboxCursorAdapter);
        content[1].setListAdapter(mArchiveCursorAdapter);
    }