Search in sources :

Example 1 with LoaderManager

use of android.support.v4.app.LoaderManager in project AndroidTraining by mixi-inc.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // ローダの管理をするオブジェクト
    LoaderManager manager = getSupportLoaderManager();
    Bundle argsForLoader = new Bundle();
    // ローダを初期化して非同期処理を開始する
    manager.initLoader(0, argsForLoader, MainActivity.this);
}
Also used : LoaderManager(android.support.v4.app.LoaderManager) Bundle(android.os.Bundle)

Example 2 with LoaderManager

use of android.support.v4.app.LoaderManager in project enroscar by stanfy.

the class LoaderSetTest method shouldWaitFor3Requests.

@Ignore
@Test
public void shouldWaitFor3Requests() throws Throwable {
    // enqueue
    getWebServer().enqueue(new MockResponse().setBody("R1"));
    getWebServer().enqueue(new MockResponse().setBody("R2"));
    getWebServer().enqueue(new MockResponse().setBody("R3"));
    final URL url = getWebServer().getUrl("/");
    final Fragment fragment = createFragment();
    final LoaderManager loaderManager = fragment.getLoaderManager();
    assertThat(loaderManager).isNotNull();
    final CountDownLatch waiter = new CountDownLatch(1);
    // describe loader
    final LoaderSet set = LoaderSet.build(getApplication()).withManager(loaderManager).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {

        @Override
        public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
            return new MyRequestBuilder<String>(getApplication()) {
            }.setUrl(url.toString()).setFormat("string").getLoader();
        }
    }, 1).withCallbacks(new LoaderSet.SetCallbacksAdapter<ResponseData<String>>() {

        @Override
        public Loader<ResponseData<String>> onCreateLoader(final int id, final Bundle args) {
            return new MyRequestBuilder<String>(getApplication()) {
            }.setUrl(url.toString()).setFormat("string").getLoader();
        }
    }, 2, 3).create();
    final LoaderSet.LoaderSetCallback callbacks = new LoaderSet.LoaderSetCallback() {

        @Override
        public void onLoadFinished(final Object[] data) {
            Log.i("ChainTest", Arrays.toString(data));
            waiter.countDown();
        }
    };
    set.init(null, callbacks);
    // TODO finish it
    waitAndAssert(new Waiter<Object[]>() {

        @Override
        public Object[] waitForData() {
            try {
                waiter.await(2, TimeUnit.SECONDS);
                return LoaderSetAccess.getResults(set);
            } catch (final InterruptedException e) {
                return null;
            }
        }
    }, new Asserter<Object[]>() {

        @SuppressWarnings("unchecked")
        @Override
        public void makeAssertions(final Object[] data) throws Exception {
            assertThat(data.length).isEqualTo(3);
            assertThat(((ResponseData<String>) data[0]).getModel()).isEqualTo("R1");
            assertThat(((ResponseData<String>) data[1]).getModel()).isEqualTo("R2");
            assertThat(((ResponseData<String>) data[2]).getModel()).isEqualTo("R3");
        }
    });
}
Also used : Loader(android.support.v4.content.Loader) Fragment(android.support.v4.app.Fragment) URL(java.net.URL) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) Bundle(android.os.Bundle) ResponseData(com.stanfy.enroscar.content.loader.ResponseData) CountDownLatch(java.util.concurrent.CountDownLatch) LoaderManager(android.support.v4.app.LoaderManager) LoaderSet(com.stanfy.enroscar.content.loader.LoaderSet) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with LoaderManager

use of android.support.v4.app.LoaderManager in project AndroidTraining by mixi-inc.

the class MainLoaderActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.ListView);
    // UIにバインドするデータのカラム名
    String[] from = { Book.COLUMN_NAME_BOOK_TITLE, Book.COLUMN_NAME_BOOK_PRICE };
    // 指定したカラムのデータを表示するViewのIDを指定します。
    int[] to = { R.id.Title, R.id.Price };
    // 第3引数のCursorはコールバックで設定されるのでnullを渡しています
    mSimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item_book, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    mListView.setAdapter(mSimpleCursorAdapter);
    // ボタンクリックでインサート処理を実行
    findViewById(R.id.ADD).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            insert();
        }
    });
    // ローダの管理をするオブジェクト
    LoaderManager loaderManager = getSupportLoaderManager();
    // ローダを初期化して非同期処理を開始する
    loaderManager.initLoader(0, null, this);
}
Also used : LoaderManager(android.support.v4.app.LoaderManager) SimpleCursorAdapter(android.support.v4.widget.SimpleCursorAdapter) OnClickListener(android.view.View.OnClickListener) View(android.view.View) ListView(android.widget.ListView)

Aggregations

LoaderManager (android.support.v4.app.LoaderManager)3 Bundle (android.os.Bundle)2 Fragment (android.support.v4.app.Fragment)1 Loader (android.support.v4.content.Loader)1 SimpleCursorAdapter (android.support.v4.widget.SimpleCursorAdapter)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ListView (android.widget.ListView)1 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)1 LoaderSet (com.stanfy.enroscar.content.loader.LoaderSet)1 ResponseData (com.stanfy.enroscar.content.loader.ResponseData)1 URL (java.net.URL)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1