我有一个用例,我在一个Completable中初始化一些全局变量,并在链的下一步(使用andThen运算符)我使用这些变量.
以下示例详细解释了我的用例
说你上课了 User
class User {
String name;
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的Observable,
private User mUser; // this is a global variable
public Observable<String> stringObservable() {
return Completable.fromAction(() -> {
mUser = new User();
mUser.name = "Name";
}).andThen(Observable.just(mUser.name));
}
Run Code Online (Sandbox Code Playgroud)
首先,我正在做一些初始化,我Completable.fromAction希望andThen操作员只有在完成后才能启动Completable.fromAction.
这意味着我希望mUser在andThen运营商启动时进行初始化.
以下是我对此观察的订阅
stringObservable()
.subscribe(s -> Log.d(TAG, "success: " + s),
throwable -> Log.e(TAG, "error: " + throwable.getMessage()));
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我收到一个错误
Attempt to read from field 'java.lang.String User.name' on a null object reference …Run Code Online (Sandbox Code Playgroud) 我有一个以模块化方式构建的Android项目.我按照干净的架构,通过在多个Gradle模块之间划分源代码来模块化项目.
这是App的结构.
此层次结构中的顶层模块App是其他模块所不依赖的模块,是应用程序的主要模块.下级模块domain和data不依赖于上App模块,其中所述App模块包括data和domain模块.我在app模块的build.gradle中添加了以下代码
implementation project(':domain')
api project(':data')
Run Code Online (Sandbox Code Playgroud)
现在,我在维护每个模块的依赖关系时遇到了一些问题.由于它们中的每一个都是一个单独的android模块,因此每个模块都有自己的模块build.gradle.该App模块可以使用data和domain模块中的类.但是,我有一些通用的类,(例如一些注释,实用程序,广播类,Dagger范围等),我想在所有模块中使用它们.但这些是我面临的问题
- 由于这些类都包含在主模块中
app,我不能在我访问这些data和domain,因为这些模块不依赖于更高层app- 我在所有层中使用的任何库(例如:RxJava)都需要包含在
build.gradle每个模块中
作为解决方案,我想添加一个更多的android模块,比如common哪个将包含我所有的通用类以及我在所有模块中使用的库.
我的所有其它模块app,domain以及data将具有该模块作为一个依赖.
implementation project(':common')
Run Code Online (Sandbox Code Playgroud)
因此,任何全局库和类都将添加到此模块中,并且每个单独的模块将只具有特定于模块的类.
这是一个好方法吗?或者有没有办法有效地解决这个问题?
java android android-gradle-plugin clean-architecture android-architecture
我在TextInputLayout主题中使用上述属性来使激活时的下划线颜色为绿色TextInputLayout。
<item name="colorControlActivated">#27e905</item>
Run Code Online (Sandbox Code Playgroud)
它工作正常,我得到以下结果
但正如您所看到的,colorControlActivated也会影响浮动提示颜色。我需要不同的颜色来显示浮动提示。有什么办法可以这样做吗?
我有一个具有以下样式的 Edittext
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_focused="true" />
<item android:color="@color/yellow" android:state_focused="false" />
<item android:color="@color/white" android:state_active="true" />
</selector>
Run Code Online (Sandbox Code Playgroud)
在我应用的 Edittext 样式中
<item name="backgroundTint">@drawable/states_edit_text</item>
Run Code Online (Sandbox Code Playgroud)
它有效。这会更改状态 normal 和focused 中的 EditTexts 底线颜色。当它处于错误状态时,我需要将色调颜色更改为红色。
就像是
<item android:state_error="true" android:color="@color/red"></item>
Run Code Online (Sandbox Code Playgroud)
但是没有称为错误的状态,我参考了其他答案,他们建议通过代码来实现。有什么方法可以使用android样式,我可以在错误状态下将EditText色调设置为红色吗?
我正在使用 firebase 实时数据库,并且正在特定存储引用中存储字符串列表。目前,添加一些东西到列表中
我正在执行以下步骤。
如果dataSnapshot.getValue();不为空则(数据库中已经存在数据,因此需要将新数据附加到它的末尾。)将远程列表保存 dataSnapshot.getValue();到本地变量。
然后将新数据添加到它的末尾
下面是我为此编写的代码
DatabaseReference reference = root()
.child(userId)
.child(list_id);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
reference.removeEventListener(this);
List<String> value = new ArrayList<>();
if (dataSnapshot.getValue() == null) {
value.add(message);
} else {
if (dataSnapshot.getValue() instanceof List && ((List) dataSnapshot.getValue()).size() > 0 && ((List) dataSnapshot.getValue()).get(0) instanceof String) {
value = (List<String>) dataSnapshot.getValue();
value.add(message);
}
}
reference.setValue(value, (databaseError, databaseReference) -> {
finish();
});
}
@Override
public void onCancelled(@NonNull …Run Code Online (Sandbox Code Playgroud) 我有一个 SQLite 数据库,我在其中执行如下查询
Select * from table where col_name NOT IN ('val1','val2')
Run Code Online (Sandbox Code Playgroud)
基本上,我从服务器获取了一个巨大的值列表,我需要选择给定列表中不存在的值。
目前工作正常,没有任何问题。但是,随着服务器数据库的频繁更新,来自服务器的值的数量变得巨大。
因此,我可能会获得数千个需要传递给的字符串值NOT IN
我的问题是,它将来会导致性能问题吗?NOT IN参数有大小限制吗?(比如您可以检查最多 10000 个值)?
它会在某个时候导致崩溃吗?
我有一个seekbar和 一个ImageView。在搜索栏移动时,我相应地缩放图像视图。下面是我的完整代码
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.app.pdf.pdfmetadata.Main2Activity">
<FrameLayout
android:background="#20000000"
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_dialog_info"/>
</FrameLayout>
<SeekBar
android:id="@+id/seekbar"
android:min="1"
android:max="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
Java代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View imageView = findViewById(R.id.imageView);
SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { …Run Code Online (Sandbox Code Playgroud) 我有一个 DataBinding 类ActivityMainBinding,其中 XML 看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<layout>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_add" />
</android.support.design.widget.CoordinatorLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
而 content_main.xml 是另一个绑定
<?xml version="1.0" encoding="utf-8"?>
<layout>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
现在,在我的活动中,我设置了这样的内容视图。
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)
现在我可以使用此绑定访问所有 XML 元素。喜欢
Toolbar toolbar = activityMainBinding.toolbar;
Run Code Online (Sandbox Code Playgroud)
但我的一些小部件在包含的布局中 ContentMainBinding
现在,我如何ContentMainBinding从 …
我使用firebase存储进行云存储
上传文件时,如果文件已存在于存储参考中,我不想上传.我只需要跳过上传并继续下一个文件.
目前我像这样上传
String pathString = "/Files/" + filename;
StorageReference filepathReference = rootreference.child(pathString);
StorageReference uploadRef = filepathReference.child(pathString);
UploadTask uploadTask = uploadRef.putFile(file);
Run Code Online (Sandbox Code Playgroud)
其中,显然再次上传并替换文件是否已存在.我只想通过检查存储桶中已存在的文件名来跳过不需要的上传.那可能吗?
android ×8
firebase ×2
java ×2
android-view ×1
data-binding ×1
mysql ×1
rx-java ×1
rx-java2 ×1
sqlite ×1