本文整理汇总了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();
}