gpt4 book ai didi

android - 来自光标加载器的 ListFragment 无法在方向更改时显示任何内容

转载 作者:行者123 更新时间:2023-11-30 04:06:02 27 4
gpt4 key购买 nike

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

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

我无法弄清楚为什么消息在旋转后没有显示。如果有任何想法或帮助,我将不胜感激。我在下面包含了我的消息传递 Activity 。

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];
}
}

最佳答案

事实证明,ViewPager 和 fragment 标签有些奇怪。我的解决方案是在 onSaveInstanceState() 中的事务中添加 fragment ,然后在 onCreate()(第二段代码)中使用生成的标签按标签获取 fragment 在 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);
}

关于android - 来自光标加载器的 ListFragment 无法在方向更改时显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11654774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com