标签: android-architecture-components

无法获取Android架构组件的资源

我通过将这些行添加到build.gradle来应用于我的项目Android Architecture Components:

// Android Architecture Lifecycle
compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"

// Android Architecture Room
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
Run Code Online (Sandbox Code Playgroud)

它工作,但在将Android Studio更新为Canary 3版本后,我仍然在编译时遇到此错误

错误:org.gradle.api.resources.ResourceException:无法获取资源' https://jitpack.io/android/arch/lifecycle/runtime/1.0.0-alpha1/runtime-1.0.0-alpha1.pom '.

错误:org.gradle.api.UncheckedIOException:无法HEAD ' https://jitpack.io/android/arch/lifecycle/runtime/1.0.0-alpha1/runtime-1.0.0-alpha1.pom '.从服务器收到状态码401:未经授权

...来自库的其他poms具有相同的错误.

我尝试重新启动Android Studio,卸载应用程序,当然还有clean-rebuild.

android android-studio android-gradle-plugin android-architecture-components

1
推荐指数
1
解决办法
1557
查看次数

无法在AppCompatActivity中调用getLifecycle()方法

我正在尝试测试android体系结构组件,并在应用程序中包含相同的库。

仔细阅读 Google文档中的示例代码,它具有方法getLifecycle(),该方法将传递给另一个类。但是我无法从扩展AppCompatActivity的主要活动中调用该方法。自动建议不会显示该方法。我想念什么吗?

android android-architecture-components

1
推荐指数
1
解决办法
1604
查看次数

Android房间迁移空错误

当我在 Android 上从旧的 sqlite 方式迁移到 Room 时,我需要使用“INTEGER NOT NULL”进行编译。问题是,当迁移发生时,您正在使用“NOT NULL”参数在新表中插入 NULL 字段,但出现错误

android.database.sqlite.SQLiteConstraintException:NOT NULL 约束失败:note.notification_state(代码 1299)

编辑:

11-29 22:52:58.891 14605-14630/com.aleksandarvasilevski.notes E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
                                                                            Process: com.aleksandarvasilevski.notes, PID: 14605
                                                                            java.lang.IllegalStateException: Migration didn't properly handle note(com.aleksandarvasilevski.notes.repository.db.Note).
                                                                             Expected:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=true, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=true, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                             Found:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-room android-architecture-components

1
推荐指数
1
解决办法
4660
查看次数

Android架构组件:ViewModel如何观察存储库中的LiveData

我正在研究Android架构组件,我有点困惑.在示例中,他们使用存储库并声明ViewModel观察到存储库的数据源内的更改.我不明白数据源中的更改是如何推送到ViewModel的,因为我看不到ViewModel中的任何代码将它们订阅到存储库.类似地,片段观察ViewModel的LiveData,但它们实际上订阅了LiveData:

 // Observe product data
    model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
        @Override
        public void onChanged(@Nullable ProductEntity productEntity) {
            model.setProduct(productEntity);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我在ViewModels中看不到任何类型的订阅来观察存储库.我错过了什么吗?

android mvvm viewmodel android-livedata android-architecture-components

1
推荐指数
1
解决办法
2321
查看次数

Android MVVM 架构组件:如何从列表片段更新单个数据实体?

我一直在努力学习新的 Android 架构组件。我的应用程序基于此示例:基本示例

我有它的工作,但我被困在如何插入、更新或删除 Room 数据库中的数据。在示例中,它没有提供这些示例。

离开这个例子,我试图弄清楚如何从 ProductListFragment 对每个产品进行更改。在我的应用程序中,我没有使用产品,而是一个定义的区域。因此,例如在列表中,我想要一个按钮,按下时将删除该区域。或者一个可以更新 Zone 属性的按钮,例如 Zone 有一个 active 属性。当按钮点击 CardView 时,它会更新区域的 active 属性。

区域列表片段

public class ZoneListFragment extends android.support.v4.app.Fragment {

    public static final String TAG = "ZoneListViewModel";
    private ZoneAdapter mZoneAdapter;
    private FragmentZoneListBinding mBinding;
    private ZoneListViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_zone_list, container, false);

        mZoneAdapter = new ZoneAdapter(mZoneClickCallback, mZoneDirectionClickCallback);
        mBinding.zonesList.setAdapter(mZoneAdapter);

        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel …
Run Code Online (Sandbox Code Playgroud)

android android-architecture-components

1
推荐指数
1
解决办法
2286
查看次数

在房间持久库中无法使用 like 子句

我在我的项目中使用房间持久库。我试图在查询中使用“like”子句,但我无法使用。我已经尝试过编译时 (Query) 和运行时查询 (Raw Query)。它既没有给出任何异常,也没有给出任何输出。

我尝试了以下方法,但都没有奏效:-

  1. 编译时间查询 -

    @Query("select * from recipe where type_of_diet like '%vegetarian%' ")
    public abstract DataSource.Factory<Integer, RecipeListPojo> 
    getRecipeListVeg();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 原始查询 -

    @RawQuery(observedEntities = RecipeListPojo.class)
    public abstract DataSource.Factory<Integer,RecipeListPojo> 
    getAllRecipesList(SupportSQLiteQuery sqLiteQuery);
    
    String query="select * from recipe  where type_of_diet like '%vegetarian%' ";
    SimpleSQLiteQuery simpleSQLiteQuery = new SimpleSQLiteQuery(query);
    recipeDao.getAllRecipesList(simpleSQLiteQuery);
    
    Run Code Online (Sandbox Code Playgroud)

compileSdkVersion 27

使用的库 - 实现 "android.arch.persistence.room:runtime:1.1.0-beta3" annotationProcessor "android.arch.persistence.room:compiler:1.1.0-beta3"

请让我知道如何运行这些类型的查询。

android android-sqlite android-room android-architecture-components

1
推荐指数
1
解决办法
3659
查看次数

调用 ft.replace 时,Android 为同一片段返回不同的 ViewModel 实例

我有一个容器 Fragment,称之为包含ContainerFragment两个FragmentFragmentAFragmentB. 两者,FragmentA并使用该方法在方法中FragmentB创建一个。ViewModelonCreate()ViewModelProviders.of(this)

假设容器一次只能显示一个片段,我正在使用该FragmentTransaction.replace()方法在FragmentA和之间切换FragmentB。在我的例子中,我显示了FragmentA,它创建了FragmentAViewModel,然后触发FragmentB了一个操作来替换FragmentA。现在,当我完成后FragmentB,我再次调用 replace 来显示FragmentA。这是我在替换片段的方法中所做的检查:

if (fragmentA == null) {
    fragmentA = FragmentA.newInstance();
}
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.container_content, fragmentA, "my-tag");
ft.commit();
Run Code Online (Sandbox Code Playgroud)

因为它FragmentA是第一次运行时创建的,所以它不会进入 if 块。但是我注意到onCreate()ofFragmentA返回了一个不同的 ViewModel 实例。我有以下代码FragmentA

public void onCreate(Bundle sI) {
    super.onCreate(sI);
    mViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
    Log.i("test", "ViewModel-> " + …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-viewmodel android-architecture-components

1
推荐指数
1
解决办法
1816
查看次数

为什么在使用JetPack Navigation导航后立即调用片段中的onDestroyView

导航到另一个片段时立即调用onDestroyView()是正常行为吗?

我调用以下方法进行导航:

findNavController().navigate(R.id.action_homefragment_to_detailsfragment)
Run Code Online (Sandbox Code Playgroud)

有办法避免这种情况吗?还是应该从ViewModel恢复所有内容?例如,我在ScrollView中的最后一个滚动位置。

android android-architecture-components android-jetpack

1
推荐指数
1
解决办法
265
查看次数

在Android中使用导航组件时如何删除默认动画过渡?

我正在使用导航组件,并且在主活动中有一个底部导航视图。当我点击该底部导航视图中的选项卡时,出现该片段时似乎动画淡入了。我认为我没有手动设置动画,似乎默认情况下该动画会存在。

我想删除该动画。这是我在主要活动中使用的代码。

class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener {

    private lateinit var navController : NavController
    lateinit var destinationTitleTextView : TextView
    lateinit var progressBar : ProgressBar
    lateinit var topToolbar : Toolbar
    lateinit var bottomNavigationView : BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        FirebaseApp.initializeApp(this)

        // Initial Setup views
        navController = Navigation.findNavController(this,R.id.nav_host_fragment)
        setupBottomNavMenu(navController)
        setupActionBar(navController)
        setUpViewDeclaration()


        // Add Listeners
        navController.addOnDestinationChangedListener(this)


    }



    private fun setUpViewDeclaration() {
        destinationTitleTextView = findViewById(R.id.destination_label_text_view)
        progressBar = findViewById(R.id.progressBar_main_activity)
        topToolbar = findViewById(R.id.top_toolbar)
        bottomNavigationView = findViewById(R.id.bottom_nav)

    }

    private fun setupBottomNavMenu(navController: NavController) {
        bottom_nav.setupWithNavController(navController) …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-architecture-components android-jetpack

1
推荐指数
2
解决办法
1158
查看次数

AndroidViewModel 与将应用程序上下文传递给 ViewModel

文件规定如下:

如果 ViewModel 需要 Application 上下文,例如查找系统服务,它可以扩展 AndroidViewModel 类并在构造函数中有一个接收 Application 的构造函数,因为 Application 类扩展了 Context。

代码示例:

class MainViewModel(application: Application) : AndroidViewModel(application) {
... 
}
Run Code Online (Sandbox Code Playgroud)

两个问题:

  1. 如果我需要传递Application给 ViewModel 的 ctor ,AndroidViewModel 如何帮助我?
  2. 再说一次,如果我需要通过Application,为什么我需要 AndroidViewModel?我可以只使用 ViewModel 并传递它Application

android android-architecture-components

1
推荐指数
1
解决办法
2285
查看次数