5

Hopefully simple java question I've just forgotten or never understood. I've been playing with Loaders in Android 4.0. The program is working as was but now I'm looking to the 'next' part. I want a standard Cursor loader and a custom AsyncTaskLoader. I'm stuck on part 1 trying to convert a cursorLoader to a Loader and return it.

@Override
    //public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    public Loader<Object> onCreateLoader(int id, Bundle args) {

        if (id == LISTVIEWLOADER) {
            String[] projection = { RunnerTable.COLUMN_ID,
                    RunnerTable.COLUMN_NAME };
            CursorLoader cursorLoader = new CursorLoader(getActivity(),
                    FanContentProvider.CONTENT_URI, projection, null, null,
                    null);


            return cursorLoader;   //HERE IS THE PROBLEM
        }
        return null;
    }

Type mismatch cannot convert from CursorLoader to Loader.

I believe can make it work with

public class MainFragment extends ListFragment implements
        //LoaderManager.LoaderCallbacks<Cursor>
        LoaderManager.LoaderCallbacks

But don't like the warning:

LoaderManager.LoaderCallbacks is a raw type. References to generic type LoaderManager.LoaderCallbacks should be parameterized

Thanks for any assistance you can provide.


@Oneway means nothing will ever escape your method, neither response nor exception. This is for two reasons:

  • technically exception is just another type of response (SOAP fault), thus it cannot be returned from a one-way method (which can't return anything)

  • often one-way methods are executed asynchronously by the web service framework (I know odes that). The framework returns immediately, so your customer might have received an empty response even before the handling of one-way method even started. When the exception is thrown, the original HTTP connection is long gone.

So if you want to propagate exceptions or timeouts, use standard SOAP method with empty response* and few faults declared explicitly. If you want to timeout your call after some time, you'll need separate thread pool and blocking waiting for response gor a given period of time.

* please do not confuse empty SOAP response (an XML document with no content, just root tag, wrapped in a SOAP envelope) with empty HTTP response (nothing was sent back). Remember that SOAP is not limited to HTTP. For example if you use JMS or e-mail transport, empty response (or fault) of ordinary two-way function is yet another message being sent from server to client. one-way method is just one reauest message and nothing sent back.

4

5 に答える 5

30

これは古いスレッドですが、まだ完全には回答されていません。この問題は tch0106 によって触れられましたが、完全には説明されていません。

同じ問題に遭遇し、インポートセクションで解決しました。簡単に言えば、サポート Loader、CursorLoader、および LoaderCallbacks を一緒に使用します。

Android ローカル ライブラリとサポート ライブラリの両方にいくつかのクラスがあります。サポート ライブラリと非サポート ライブラリ バージョンを混在させないでください。

// Use these together
import android.support.v4.content.CursorLoader;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;

// Or use these, but don't mix and match
import android.content.CursorLoader;
import android.content.Loader;
import android.app.LoaderManager.LoaderCallbacks;
import android.widget.SimpleCursorAdapter;
于 2013-06-14T13:56:52.543 に答える
7

インポートを確認してください。次の 2 つを誤って使用している可能性があります。

import android.content.CursorLoader;

import android.support.v4.content.CursorLoader;
于 2012-12-02T06:19:00.290 に答える
2

変化する

public Loader<Object> onCreateLoader(int id, Bundle args) {

  public Loader<? extends Object> onCreateLoader(int id, Bundle args) {

最初のステートメントはjvmに、returntypeはconcreteであることを伝えますObject。新しいステートメントは、jvmに。の任意のTYPEであることを通知しますObject

このチュートリアルをお読みください。

于 2012-10-24T21:16:05.887 に答える
-1

これを見てください: Android AsyncTaskLoader リファレンス

あなたの問題は次の行にあると思います:

public class MainFragment extends ListFragment implements
    //LoaderManager.LoaderCallbacks<Cursor>
    LoaderManager.LoaderCallbacks

次のように変更する必要があります。

public static class AppListFragment extends ListFragment
    implements LoaderManager.LoaderCallbacks<List<AppEntry>>
于 2013-05-21T09:54:59.363 に答える