本文整理匯總了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();
}
});
}
示例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);
}
}
});
}
示例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);
}
示例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();
}
});
}
示例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());
}
});
}
示例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();
}
示例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();
}