當前位置: 首頁>>代碼示例>>Java>>正文


Java LiveData.observe方法代碼示例

本文整理匯總了Java中android.arch.lifecycle.LiveData.observe方法的典型用法代碼示例。如果您正苦於以下問題:Java LiveData.observe方法的具體用法?Java LiveData.observe怎麽用?Java LiveData.observe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.arch.lifecycle.LiveData的用法示例。


在下文中一共展示了LiveData.observe方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: ClaimItemAdapter

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
public ClaimItemAdapter(
        final Context context,
        final LifecycleOwner owner,
        final LiveData<List<ClaimItem>> liveItems) {

    this.layoutInflater = LayoutInflater.from(context);
    this.itemPresenter = new ItemPresenter(context);

    liveItems.observe(owner, new Observer<List<ClaimItem>>() {
        public void onChanged(final List<ClaimItem> claimItems) {
            ClaimItemAdapter.this.items = (claimItems != null)
                    ? claimItems
                    : Collections.<ClaimItem>emptyList();

            ClaimItemAdapter.this.notifyDataSetChanged();
        }
    });
}
 
開發者ID:PacktPublishing,項目名稱:Hands-On-Android-UI-Development,代碼行數:19,代碼來源:ClaimItemAdapter.java

示例2: ClaimItemAdapter

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
public ClaimItemAdapter(
        final Context context,
        final LifecycleOwner owner,
        final LiveData<List<ClaimItem>> liveItems) {

    this.layoutInflater = LayoutInflater.from(context);
    this.itemPresenter = new ItemPresenter(context);

    liveItems.observe(owner, new Observer<List<ClaimItem>>() {
        @Override
        public void onChanged(final List<ClaimItem> claimItems) {
            if (!items.isEmpty()) {
                updateCommand.exec(Pair.create(items, claimItems));
            } else {
                createDisplayListCommand.exec(claimItems);
            }
        }
    });
}
 
開發者ID:PacktPublishing,項目名稱:Hands-On-Android-UI-Development,代碼行數:20,代碼來源:ClaimItemAdapter.java

示例3: setupObservers

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
protected void setupObservers() {
    LiveData<Resource<ConversationMetaData>> meta = viewModel.getMetaData();
    meta.observe(this, resource -> {
        binding.get().setConversationMetaData(resource == null || resource.data == null ? null : resource.data);
        binding.get().setConversationResource(resource);
        if (resource != null & resource.data != null) {
            String name = resource.data.getName();
            if (!TextUtils.isEmpty(name)) {
                ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(name);
            } else {
                // TODO: set usernames or something
            }

        }
        binding.get().executePendingBindings();
    });

    LiveData<ChatMessage> convo = viewModel.getConversation();
    convo.observe(this, data -> {
        if (data != null) {
            adapter.get().upsert(data);

        }
        binding.get().executePendingBindings();
    });
}
 
開發者ID:hbmartin,項目名稱:firebase-chat-android-architecture-components,代碼行數:27,代碼來源:ConversationFragment.java

示例4: onActivityCreated

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    repoViewModel = ViewModelProviders.of(this, viewModelFactory).get(RepoViewModel.class);
    Bundle args = getArguments();
    if (args != null && args.containsKey(REPO_OWNER_KEY) &&
            args.containsKey(REPO_NAME_KEY)) {
        repoViewModel.setId(args.getString(REPO_OWNER_KEY),
                args.getString(REPO_NAME_KEY));
    } else {
        repoViewModel.setId(null, null);
    }
    LiveData<Resource<Repo>> repo = repoViewModel.getRepo();
    repo.observe(this, resource -> {
        binding.get().setRepo(resource == null ? null : resource.data);
        binding.get().setRepoResource(resource);
        binding.get().executePendingBindings();
    });

    ContributorAdapter adapter = new ContributorAdapter(dataBindingComponent,
            contributor -> navigationController.navigateToUser(contributor.getLogin()));
    this.adapter = new AutoClearedValue<>(this, adapter);
    binding.get().contributorList.setAdapter(adapter);
    initContributorList(repoViewModel);
}
 
開發者ID:googlesamples,項目名稱:android-architecture-components,代碼行數:26,代碼來源:RepoFragment.java

示例5: observerPersonListResults

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
private void observerPersonListResults(LiveData<List<Person>> personsLive) {
    //observer LiveData
    personsLive.observe(this, new Observer<List<Person>>() {
        @Override
        public void onChanged(@Nullable List<Person> person) {
            if(person == null){
                return;
            }
            Toast.makeText(MainActivity.this, "Number of person objects in the response: "+person.size(), Toast.LENGTH_LONG).show();
        }
    });
}
 
開發者ID:srinurp,項目名稱:AndroidRoom,代碼行數:13,代碼來源:MainActivity.java

示例6: observePersonByMobile

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
private void observePersonByMobile(LiveData<Person> personByMob){
    personByMob.observe(this, new Observer<Person>() {
        @Override
        public void onChanged(@Nullable Person person) {
            if(person == null){
                return;
            }
            ((TextView)findViewById(R.id.name)).setText(person.getName());
            ((TextView)findViewById(R.id.email)).setText(person.getEmail());
            ((TextView)findViewById(R.id.mobile)).setText(person.getMobile());
            ((TextView)findViewById(R.id.lineOne)).setText(person.getAddress().getLineOne());
            ((TextView)findViewById(R.id.city)).setText(person.getAddress().getCity());
            ((TextView)findViewById(R.id.country)).setText(person.getAddress().getCountry());
            ((TextView)findViewById(R.id.zip)).setText(person.getAddress().getZip());
        }
    });
}
 
開發者ID:srinurp,項目名稱:AndroidRoom,代碼行數:18,代碼來源:MainActivity.java

示例7: testSubscribe

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
@Test
public void testSubscribe() {
	prepareRapid();
	RapidCollectionReference<Car> collection = Rapid.getInstance().collection("android_instr_test_003_" + UUID.randomUUID().toString(), Car.class);

	LiveData<List<RapidDocument<Car>>> liveData = RapidLiveData.from(collection, error -> {
		fail(error.getMessage());
	});

	liveData.observe(mLifecycleOwner, rapidDocuments -> {
		assertNotNull(rapidDocuments);
		unlockAsync();
	});
	lockAsync();

	collection.newDocument().mutate(new Car("asda", 1))
			.onSuccess(() -> unlockAsync())
			.onError(error -> fail(error.getMessage()));
	lockAsync();
}
 
開發者ID:rapid-io,項目名稱:rapid-io-android,代碼行數:21,代碼來源:LiveDataTest.java

示例8: testError

import android.arch.lifecycle.LiveData; //導入方法依賴的package包/類
@Test
public void testError() {
	prepareRapid(false);
	RapidCollectionReference<Car> collection = Rapid.getInstance().collection("android_instr_test_003_" + UUID.randomUUID().toString(), Car.class);

	LiveData<List<RapidDocument<Car>>> liveData = RapidLiveData.from(collection, error -> {
		assertEquals(error.getType(), RapidError.ErrorType.PERMISSION_DENIED);
		unlockAsync();
	});

	liveData.observe(mLifecycleOwner, rapidDocuments -> {
		fail("Should not get any data");
		unlockAsync();
	});
	lockAsync();
}
 
開發者ID:rapid-io,項目名稱:rapid-io-android,代碼行數:17,代碼來源:LiveDataTest.java


注:本文中的android.arch.lifecycle.LiveData.observe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。