使用的第一个版本RxJava和RxAndroid我有以下class为EventBus:
public class RxBus {
private static RxBus instance;
private PublishSubject<Object> subject = PublishSubject.create();
public static RxBus instanceOf() {
if (instance == null) {
instance = new RxBus();
}
return instance;
}
public void setMessage(Object object) {
subject.onNext(object);
}
public Observable<Object> getEvents() {
return subject;
}
}
Run Code Online (Sandbox Code Playgroud)
instanceOf在任何类中获取实例 ,我使用setMessage方法发出消息并使用代码来获取发出的消息:
bus.getEvents().subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
if (o instanceof String) {
//TODO
}
}
});
Run Code Online (Sandbox Code Playgroud)
Action1来自 …
在迁移到Kotlinfrom 时Java我遇到了一个问题。我覆盖了Object的finalize()方法:
@Override
protected void finalize() throws Throwable {
stopTimer();
super.finalize();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用 Kotlin 做同样的事情时,我找到了解决方案。第一个来自文档:
protected fun finalize() {
stopTimer()
super.finalize()
}
Run Code Online (Sandbox Code Playgroud)
文章中的第二个(俄语):
@Suppress("ProtectedInFinal", "Unused")
protected fun finalize() {
stopTimer()
super.finalize()
}
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,我都不能super.finalize()根据 IDE调用,正如它所说的unresolved reference:finalize
也许有人知道如何完成这项工作Kotlin?谢谢。
从Mirgating Java到Kotlin我尽量使用静态函数Data Binding:
<data>
<import type="com.package.domain.tools.helper.StringValidator"/>
...
</data>
Run Code Online (Sandbox Code Playgroud)
然后我调用函数hideNumber:
<com.hastee.pay.ui.view.Text
...
android:text='@{StringValidator.hideNumber(account.number)}'
app:layout_constraintRight_toRightOf="@+id/number"
app:layout_constraintTop_toBottomOf="@+id/number" />
Run Code Online (Sandbox Code Playgroud)
在此使用数据绑定会导致错误:
[kapt] An exception occurred:
android.databinding.tool.util.LoggedErrorException: Found data binding
errors.
****/ data binding error ****msg:cannot find method
hideNumber(java.lang.String) in class
com.package.domain.tools.helper.StringValidator....
Run Code Online (Sandbox Code Playgroud)
这是这个对象:
object StringValidator {
...
fun hideNumber(number: String): String {
return "****" + number.substring(number.length - 4)
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用Kotlin和达到此功能Data Binding?
我有这样的 JSON:
{
"success":true,
"data":[
{
"id":"29",
"name":"\u0420\u0435\u0441\u0442\u043e\u0440\u0430\u0446\u0456\u044f \u0411\u0430\u0447\u0435\u0432\u0441\u044c\u043a\u0438\u0445 \/ Baczewski Restaurant",
"street":"\u0432\u0443\u043b. \u0428\u0435\u0432\u0441\u044c\u043a\u0430, 8",
"latitude":"49.842292845502",
"longitude":"24.029848249565",
"image":"https:\/\/i.onthe.io\/j9aocq72r2lfsmoh9.r500x500.01ff9fff.jpg"
},
...
]
}
Run Code Online (Sandbox Code Playgroud)
根据它必须 Classes(pojo)用模式创建到pojo。第一个提供了从data数组中获取数据的方法 - >
public List<Datum> getData() {
return data;
}
Run Code Online (Sandbox Code Playgroud)
第二个是model 这个数据的一个。
在仅使用改造 2.0 时,我执行 a call,object 从 data数组中解析每个并将其添加到RecyclerView.Adapter
Call<PlaceList> call = service.list(1, offset, 10);
call.enqueue(new Callback<PlaceList>() {
@Override
public void onResponse(Call<PlaceList> call, Response<PlaceList> response) {
final int size = response.body().getData().size();
for (int i = 0; i < size; …Run Code Online (Sandbox Code Playgroud) 我有一列三行:
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
print('card of course clicked');
},
child: Card(
shape: RoundedRectangleBorder(borderRadius:
BorderRadius.circular(8.0)),
child: Container(
height: 144,
child: Row(children: [
Padding(
padding: EdgeInsets.all(16.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
child: Image.network(_model._schoolThumb))),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Align(
alignment: Alignment.center,
child: Text(_model._schoolName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18)),
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.location_on, color: Colors.grey[700]),
Container(width: 8),
Text(_model._guides,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400)),
],
),
SizedBox(height: …Run Code Online (Sandbox Code Playgroud) 我需要找到数组中数字之间最长的方式(从大到小).我试着写recursive功能并得到了java.lang.StackOverflowError,但由于缺乏知识,我不明白为什么会发生这种情况.
首先,我初始化数组并用随机数填充它:
public long[] singleMap = new long[20];
for (int i = 0; i < 20; i++) {
singleMap[i] = (short) random.nextInt(30);
}
Run Code Online (Sandbox Code Playgroud)
于是,我试图找到掰着指头数最长的途径(例如{1,4,6,20,19,16,10,6,4,7,6,1 ...})并返回计数等数字.
public int find(long[] route, int start) {
if (route[start] > route[start + 1]) {
find(route, start++);
} else {
return start;
}
return start;
}
Run Code Online (Sandbox Code Playgroud)
所以这是日志:
08-23 13:06:40.399 4627-4627/itea.com.testnotification I/dalvikvm: threadid=1: stack overflow on call to Litea/com/testnotification/MainActivity;.find:ILI
08-23 13:06:40.399 4627-4627/itea.com.testnotification I/dalvikvm: method requires 36+20+12=68 bytes, fp is 0x4189e318 (24 …Run Code Online (Sandbox Code Playgroud) 我有一个启动器屏幕,我可以在其中检查是否有特定数据。根据结果我显示不同的屏幕
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LauncherScreen()
);
}
}
class LauncherScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("build, build");
Future.delayed(new Duration(milliseconds: 2000), () {
LocalData localData = LocalData();
localData.getCity().then((city) {
if (city != null) {
Const.city = city;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
} else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SelectCities()),
);
}
});
});
return Container( …Run Code Online (Sandbox Code Playgroud) 我有一个带有文本的列。我不知道传入字符串的确切大小,所以我希望我的一些文本以省略号结尾。
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 16),
Align(
alignment: Alignment.center,
child: Text(_model._schoolName,
overflow: TextOverflow.ellipsis, //this one works
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w600,
fontSize: 18)),
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.location_on, color: Colors.grey[700]),
Container(width: 8),
Text(_model._guides,
overflow: TextOverflow.ellipsis, //this one doesn't work
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400)),
],
),
SizedBox(height: 12),
Row(
children: <Widget>[
Icon(Icons.attach_money, color: Colors.grey[700]),
Container(width: 8),
Text(_model._priceFrom,
style: TextStyle(
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight.w400))
],
)
],
),
) …Run Code Online (Sandbox Code Playgroud)