当前位置: 首页>>代码示例>>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;未经允许,请勿转载。