首先,我要求对我的英语不好道歉.
我开发了很多年的Java SE软件,而且我曾经使用过MVC设计模式.现在我开发了Android应用程序,我不满意那个说已经使用了MVC模式,xml文件充当视图的论点.
我在网上做了很多研究,但似乎对这个话题没有一致意见.有些人使用MVC模式,有些则使用MVP模式,但我认为,没有一致意见.
最近我买了一本书(Android Best Practices,来自Godfrey Nolan,Onur Cinar和David Truxall),在第二章中,您可以找到MVC,MVVM和依赖注入模式.在尝试了所有这些之后,我认为对于我的应用程序和我的工作模式,最好的是MVVM模式.
我发现这种模式在使用活动进行编程时非常容易使用,但是我对使用片段编程时如何使用它感到困惑.我将重现应用于简单"todo app"的MVVM模式示例,该模板是从"Android Best Practices"一书的网站下载的.
观点(活动)
package com.example.mvvm;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class TodoActivity extends Activity
{
public static final String APP_TAG = "com.logicdrop.todos";
private ListView taskView;
private Button btNewTask;
private EditText etNewTask;
private TaskListManager delegate;
/*The View handles UI setup only. All event logic and delegation
*is handled by the ViewModel.
*/
public static interface TaskListManager
{
//Through this …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 iOS 应用程序,它使用 Kotlin Native 共享模块。这个Kotlin Native共享模块,利用了自主开发的iOS框架。
\n这在过去非常有效,但现在我正在尝试将我的项目升级为最新版本的 Kotlin Native(此时为 1.4.10)、Android Studio 等,但我无法导入我的自定义 iOS 框架依赖项。
\n\n\n按照https://kotlinlang.org/docs/mobile/add-dependency.html上的指南,我向我的共享代码项目添加了一个 .def 文件,其中包含以下内容:
\nlanguage = Objective-C\nmodules = SlicerUtils\npackage = com.xxxx.customframework\nRun Code Online (Sandbox Code Playgroud)\nbuild.gradle.kts 文件是使用 Android Studio 最新版本 (4.2.idontremember) 中提供的 KMM 项目模板生成的标准文件。然后我添加了 iosX64 和 iosArm64 部分,如上面链接中所述:
\nimport org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget\n\nplugins {\n id("com.android.library")\n kotlin("multiplatform")\n kotlin("plugin.serialization") version "1.4.10"\n\n\n\n id("kotlin-android-extensions")\n\n}\ngroup = "con.xxxx.customframework"\nversion = "1.0"\n\nrepositories {\n gradlePluginPortal()\n google()\n jcenter()\n mavenCentral()\n}\nkotlin {\n android()\n ios {\n binaries {\n framework {\n baseName = "shared"\n }\n }\n }\n\n\n\n\n\n …Run Code Online (Sandbox Code Playgroud) 我按照新的教程在Android上实现GCM客户端,我无法让它工作.
我成功地获得了令我的设备的令牌,当我发送以下消息时
{
"to": "f19jCbaw-S4:APA91b....",
"notification": {
"body": "body",
"title": "title"
},
"data": {
"message": "test"
}
}
Run Code Online (Sandbox Code Playgroud)
到https://gcm-http.googleapis.com/gcm/send,我得到了结果:
{
"multicast_id": 844556567...,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1433092445706613%..."
}
]
}
Run Code Online (Sandbox Code Playgroud)
但我的应用程序永远不会进入MyGcmListenerService类的onMessageReceived.我唯一的线索是以下日志跟踪:
6399-6454/com.miapp.app W/GcmNotification? Failed to show notification: Missing icon
Run Code Online (Sandbox Code Playgroud)
我一步一步地重复了 Google Cloud Messaging Quickstart示例项目.很糟糕,我使用我的api密钥和我的发件人ID ...但是id不起作用,我的app永远不会停留在我的onMessageReceived方法中.
还有其他人有这个问题吗?
我不确定我的项目是否有问题,或者我不明白绿道是如何工作的.可能第二种选择是正确的选择.
我有以下实体:
Entity review= schema.addEntity("Review");
review.setTableName("reviews");
review.addIdProperty().primaryKey();
Property user_id = review.addLongProperty("user_id").getProperty();
Property object_id= review.addLongProperty("object_id").getProperty();
review.addStringProperty("text");
Index indexUnique = new Index();
indexUnique.addProperty(user_id);
indexUnique.addProperty(object_id);
indexUnique.makeUnique();
review.addIndex(indexUnique);
Run Code Online (Sandbox Code Playgroud)
我在字段user_id和object_id上使用了一个唯一的索引,因为我希望这两个字段作为主键使用,因为我只希望对每个用户和表中的每个对象进行一次审阅.
我第一次在数据库中插入评论时,执行以下操作:
MyEntity e=new MyEntity();
e.setText("Hello");
e.setUserId(1);
e.setObjectId(9);
myDao.insert(e);
Run Code Online (Sandbox Code Playgroud)
一切都在那里工作.下一步是用户更新实体,我使用以下代码:
MyEntity e=new MyEntity();
e.setText("New text");
e.setUserId(1);
e.setObjectId(9);
myDao.insertOrReplaceInTx(e);
Run Code Online (Sandbox Code Playgroud)
insertOrReplaceInTx方法无异常地工作.当我尝试使用myDao.loadAll()方法加载所有评论时,会出现问题.出现以下异常:
无法从CursorWindow读取第0行,第0列
我查看了数据库文件,问题是实体已保存到表中,但所有字段都为空,我不知道为什么.
在做了很多测试后,我设法让它工作,执行以下操作.第一次创建评论时,我也是这样做的:
MyEntity e=new MyEntity();
e.setText("New text");
e.setUserId(1);
e.setObjectId(9);
myDao.insert(e);
Run Code Online (Sandbox Code Playgroud)
稍后,如果实体已存在于数据库中(对于编辑操作),那么我加载实体,而我这样做;
MyEntity e= do a query with greendao and load the entity with user_id=1 and object_id=9//Yes I know that is pseudocode
e.setText("New text");
myDao.update(e);
Run Code Online (Sandbox Code Playgroud)
它有效,但奇怪的是我不需要调用myDao.update(e); 我已经意识到在e.setText("New …