永远不会调用onLoadFinished

时间:2014-01-13 06:25:43

标签: android android-support-library android-cursorloader

似乎支持库v4中存在一个错误,我想使用CursorLoader和LoaderManager访问MediaStore音频,我已经遵循协议到T,无论我做什么,都不会调用OnLoaderFinished。

以下是一些代码片段,我创建了一些名为MediaStoreInterface的内容,

MediaStoreInterface的相关部分如下:

public class MediaStoreInterface extends Observable
{
protected Cursor cursor;
protected static final int LOADER_ID1 = 1;
protected static final int LOADER_ID2 = 2;
protected static int lastLoaderId = 0; 
protected static String function = "SONGS"; 
protected static PaudioActivity act;
protected static Sound selectedSound = null;
protected static final String[] projections = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA};
protected static final String[] projectionsPlaylist = {MediaStore.Audio.Playlists._ID,MediaStore.Audio.Playlists.NAME};
protected static final Uri sourceUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
protected static final Uri uriPlaylists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
protected static ArrayList<String> ret;
protected static boolean done = false;
//Singleton 

protected static MediaStoreInterface ref = null;


public static MediaStoreInterface getMusicLibrary(Activity act_)
{
    if (ref == null)
        ref = new MediaStoreInterface(act_);

    return ref;     
}

protected MediaStoreInterface(Activity act_)
{
    act = (PaudioActivity) act_;        
}
public static MediaStoreInterface getMSI() 
{
    if (android.os.Build.VERSION.SDK_INT < 11)
        return MediaStoreInterfaceFrodo.getMusicLibrary(act);
    else
        return MediaStoreInterfaceHoneyComb.getMusicLibrary(act);
}



protected void loadArray(Cursor data) 
{
    int num = data.getColumnCount();
    if (data == null || !data.moveToFirst())
        return;
    while(!data.isLast())
    {
        String s  = data.getString(0);
        int col = 1;
        while(col < num)
            s += "|" + data.getString(col++);
        ret.add(s);
        data.moveToNext();          
    }

    if (function == "SONGS")
        numSongs = ret.size();
    if (function == "PLAYLISTS")
        numPlaylists = ret.size();
    cursor = data;  
}
public static void setDone() 
{
   done = true;     
}



}

它是一个Singleton,应该为每个API返回正确的MediaStoreInterface,   我将Frodo从这里扩展如下:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.Loader.OnLoadCompleteListener;






    public class MediaStoreInterfaceFrodo extends MediaStoreInterface implements LoaderManager.LoaderCallbacks<Cursor>, OnLoadCompleteListener<Cursor>
    {

        public static MediaStoreInterface getMusicLibrary(Activity act_)
        {
            if (ref == null)
                ref = new MediaStoreInterfaceFrodo(act_);

            return ref;     
        }

        public void setPaudioActivity(PaudioActivity act_)
        {
            act = act_;
        }

        protected MediaStoreInterfaceFrodo(Activity act_) 
        {
            super(act_);
        }

        public ArrayList<String> getSongList() 
        {
            function = "SONGS";
            done = false; 
            if (ret == null)
                ret = new ArrayList<String>();
            else
                ret.clear();
            lastLoaderId = LOADER_ID1;
            LoaderManager lm = act.getSupportLoaderManager(); 
            Loader<Cursor> loader1 = lm.getLoader(LOADER_ID1);
            if (loader1 != null && loader1.isReset())
                loader1 = lm.restartLoader(LOADER_ID1, null, this);
            else
            {
                loader1 = lm.initLoader(LOADER_ID1, null, this);    
            }

            while(!done)
            {
                waitabit();
            }

            return ret; 
        }

        public static void editSelectedSongTitle(String newtitle) 
        {
            // TODO Auto-generated method stub      
        }



        public static void setContinuePlayMode(PlayMode pm_) 
        {
            pm = pm_;       
        }

        public List<String> getPlayListNames() 
        {
            function = "PLAYLISTS";
            done = false; 
            if (ret == null)
                ret = new ArrayList<String>();
            else
                ret.clear();
            lastLoaderId = LOADER_ID2;
            LoaderManager lm = act.getSupportLoaderManager(); 
            lm.initLoader(LOADER_ID2, null,  this);

            while(!done)
            {
                waitabit();
            }

            function = "SONGS";

            return ret;

        }





        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle arg1) 
        {
            CursorLoader cursorLoader = null;

            switch (id) 
            {
            case LOADER_ID1:
                cursorLoader = new CursorLoader(act, sourceUri, projections, null, null, MediaStore.Audio.Media.TITLE); 
                break;
            case LOADER_ID2:
                cursorLoader = new CursorLoader(act, uriPlaylists, projectionsPlaylist, null, null,  MediaStore.Audio.Playlists.NAME);
                break;
            default:
                break;
            }
            cursorLoader.forceLoad();
            return cursorLoader;

        }

        @Override
        public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor data) 
        {
            cursor = data;
            loadArray(cursor);
            done = true;

        }

        @Override
        public void onLoaderReset(Loader<Cursor> arg0) 
        {

            act.getSupportLoaderManager().restartLoader(lastLoaderId, null,  ( android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this);

        }

        @Override
        public void onLoadComplete(Loader<Cursor> loader, Cursor data) 
        {
            onLoadFinished( loader, data);

        }




    }

我试图将数据库查询与我活动中常见的核心和碎片分开,长话短说我有一个按钮,如果单击,则调用getSongList,  我还有setActivity将act成员转换为调用Activity,

我已经阅读了所有相关的StackOverflow文章和Android开发网站上的所有内容。有些人可能会反对我必须在UI线程上调用我的一些函数,我也这样做了,但没有用。我在我的主Activity中实现了相同的代码集,但不管我做什么。永远不会调用OnLoadFinished。

2 个答案:

答案 0 :(得分:3)

我不确定导致问题的原因,因为您的代码不完整,但是您的导入意味着您可能不会使用支持包中的活动:

  

import android.app.Activity;

应该是

  

import android.support.v4.app.FragmentActivity;

我实际上看不到你的代码如何编译,因为非支持活动没有一个名为getSupportLoaderManager()的方法

在不知道实际问题是什么的情况下,我将发布我刚刚创建的示例,我将代码归结为实际需要的内容。但请注意,您不必在活动中实现LoaderCallback。正如我测试的那样,这段代码正在运行。

package com.suhw.samples.mediastore;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;

public class QueryMediaStoreActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    private final static String TAG = "QueryMediaStoreActivity";
    private static final int LOADER_ID1 = 1;
    private static final String[] audioMediaProjection = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA};
    private static final Uri audioMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LoaderManager loaderManager = getSupportLoaderManager();
        loaderManager.initLoader(LOADER_ID1, null, this);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
        switch (id) {
            case LOADER_ID1:
                return new CursorLoader(this, audioMediaUri, audioMediaProjection, null, null, MediaStore.Audio.Media.TITLE);
            default:
                return null;
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        switch (cursorLoader.getId()) {
            case LOADER_ID1:
                Log.d(TAG, "onLoadFinished called:\n\tcursorCount:" + cursor.getCount());
                break;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
    }
}

答案 1 :(得分:1)

更改 OncreateLoader (),如果处理多个Loader,请更好地使用switch()语句。

@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arg1) 
    {
        CursorLoader cursorLoader = null;

        switch (id) {
        case LOADER_ID1:
            cursorLoader = new CursorLoader(getActivity(), sourceUri, projections, null, null, MediaStore.Audio.Media.TITLE); 
            break;
        case LOADER_ID2:
            cursorLoader = new CursorLoader(getActivity(), uriPlaylists, projectionsPlaylist, null, null,  MediaStore.Audio.Playlists.NAME);
            break;
        default:
            break;
        }
        cursorLoader.forceLoad();
        return cursorLoader;

    }

编辑: -

使用getActivity()代替act了解详情CursorLoader

注意: - 方法getLoaderManager()仅在Fragment类中可用。要在FragmentActivity中获取LoaderManager,请调用getSupportLoaderManager()。

试试这样。