我想实现分页以在我的应用程序中显示我想要的项目视图的大部分列表。这就是为什么我选择使用Google新发布的分页库,即Paging库3。我在我的应用程序中使用Rxjava、Livedata和ViewModel。实现分页库后,我遇到了一个奇怪的问题。当我调用获取列表的方法时,它会一次又一次地调用,并且不会停止调用。事实上,尽管我没有滚动列表,但它会自动增加页码。这是我尝试过的代码
JobListRestApi.kt
interface JobListRestApi {
@GET("job/list")
fun getJobResponse(
@Query("page") pageNumber: Int
): Single<Response<JobResponse>>
}
Run Code Online (Sandbox Code Playgroud)
JobListDataSource.kt
class JobListDataSource @Inject constructor(
private val jobListRestApi: JobListRestApi
): RxPagingSource<Int, Job>() {
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, Job>> {
val position = params.key ?: 1
return jobListRestApi.getJobResponse(position).toSingle()
.subscribeOn(Schedulers.io())
.map { jobResponse -> jobResponse.jobData.jobs }
.map { jobs -> toLoadResult(jobs, position) }
.onErrorReturn { LoadResult.Error(it) }
}
private fun toLoadResult(data: ArrayList<Job>, position: Int): LoadResult<Int, Job> {
val prevKey = if (position == 1) null else position-1 …
Run Code Online (Sandbox Code Playgroud) android mvvm rx-java android-livedata android-paging-library
我从分页 2 迁移到分页 3。我尝试将分页 2 的 ItemKeyedDataSource 实现到分页库 3。但我面临的问题是,在加载的两个连续页面中,相同的值(currentJodId)作为 nextkey 传递。在该应用程序崩溃之后。但是如果我在数据源中添加“keyReuseSupported = true”,应用程序不会崩溃。但它开始调用与 nextkey 相同的项目 ID。
JobSliderRestApi.kt
@GET("job/list/slides")
fun getDetailOfSelectedJob(
@Query("current_job") currentJodId: Int?,
@Query("limit") jobLimit: Int?,
@Query("search_in") fetchType: String?
): Single<Response<JobViewResponse>>
Run Code Online (Sandbox Code Playgroud)
JobViewResponse.kt
data class JobViewResponse(
@SerializedName("data") val data: ArrayList<JobDetail>?
) : BaseResponse()
Run Code Online (Sandbox Code Playgroud)
JobDetail.kt
data class JobDetail(
@SerializedName("job_id") val jobId: Int,
@SerializedName("tuition_type") val jobType: String?,
@SerializedName("class_image") val jobImage: String,
@SerializedName("salary") val salary: String,
@SerializedName("no_of_student") val noOfStudent: Int,
@SerializedName("student_gender") val studentGender: String,
@SerializedName("tutor_gender") val preferredTutor: String,
@SerializedName("days_per_week") val daysPerWeek: String?,
@SerializedName("other_req") val …
Run Code Online (Sandbox Code Playgroud) 我想在我的 flutter 应用程序中使用共享首选项实现会话管理系统。对于依赖注入,我使用 GetIt 库。但是当我运行应用程序时,它显示“flutter:创建会话时出错”“构建构建器(脏)抛出以下 ArgumentError:无效参数(SharedPreferences 类型的对象未在 GetIt 内注册。您是否忘记传递实例名称?(您是否不小心执行了 GetIt sl=GetIt.instance(); 而不是 GetIt sl=GetIt.instance;)):SharedPreferences'
Session.dart
abstract class Session {
void storeLoginInfo(String accessToken);
bool isUserLoggedIn();
String getAccessToken();
void deleteLoginInfo();
}
Run Code Online (Sandbox Code Playgroud)
SessionImpl.dart
class SessionImpl extends Session {
SharedPreferences sharedPref;
SessionImpl(SharedPreferences sharedPref) {
this.sharedPref = sharedPref;
}
@override
void storeLoginInfo(String accessToken) {
sharedPref.setBool('login_status', true);
sharedPref.setString('access_token', accessToken);
}
@override
bool isUserLoggedIn() {
final isLoggedIn = sharedPref.getBool('login_status') ?? false;
return isLoggedIn;
}
@override
String getAccessToken() {
return sharedPref.getString('access_token') ?? "";
}
@override
void deleteLoginInfo() { …
Run Code Online (Sandbox Code Playgroud) 在我的一个应用程序中,我使用 Android 导航架构组件。一切都很顺利。但当我想导航到 DialogFrgment 时,我陷入了困境。虽然触发了DialogFragment,但没有看到布局。只能看到重叠模糊背景。无法弄清楚实际问题。这是我用来显示dialogFragment的代码。
这是我想展示的自定义布局
terms_and_condition.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
tools:ignore="WebViewLayout">
<LinearLayout
android:id="@+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="@dimen/_60sdp"
android:background="@color/colorAccent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_20sdp"
android:layout_weight="3"
android:text="@string/terms_amp_conditions"
android:textSize="@dimen/_15ssp"
android:textStyle="bold"
android:textColor="@color/white" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/close_privacy_policy_dialog_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_close" />
</LinearLayout>
<WebView
android:id="@+id/privacy_policy_web_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_300sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout6"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是对话框片段的基类
BaseDialogFragment.kt
abstract class BaseDialogFragment : DaggerAppCompatDialogFragment() {
@LayoutRes
abstract fun getLayoutResource(): Int
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? …
Run Code Online (Sandbox Code Playgroud) 我想在我的 Flutter 应用程序中实现 GraphQL 客户端。对于依赖注入,我使用 GetIt 库。但是当我运行应用程序时,它说
'无效参数(HomeGraphQLService 类型的对象未在 GetIt 中注册。您是否忘记传递实例名称?(您是否不小心执行了 GetIt sl=GetIt.instance(); 而不是 GetIt sl=GetIt.instance;)):HomeGraphQLService '
.
这意味着 GraphQL 客户端没有以某种方式实例化,尽管我在我的服务定位器中注册了它
Session.dart
abstract class Session {
String getAccessToken();
}
Run Code Online (Sandbox Code Playgroud)
SessionImpl.dart
class SessionImpl extends Session {
SharedPreferences sharedPref;
SessionImpl(SharedPreferences sharedPref) {
this.sharedPref = sharedPref;
}
@override
String getAccessToken() {
return sharedPref.getString('access_token') ?? "";
}
}
Run Code Online (Sandbox Code Playgroud)
GraphQLClientGenerator.dart
class GraphQLClientGenerator {
Session session;
GraphQLClientGenerator(Session session) {
this.session = session;
}
GraphQLClient getClient() {
final HttpLink httpLink = HttpLink('https://xxx/graphql');
final AuthLink authLink = …
Run Code Online (Sandbox Code Playgroud) 最近我正在使用 Spring boot 框架和 Kotlin。使用 GET 方法一切都OK。但是,当使用 POST 方法注册新用户时,我遇到了一些问题,状态代码为 400 的错误请求。这是与我的 Spring Boot 项目关联的代码
User.kt
@Entity
@Table(name = "user_info")
data class User(
@Id
@SequenceGenerator(
name = "user_seq",
sequenceName = "user_seq",
allocationSize = 1
)
@GeneratedValue(
strategy = SEQUENCE,
generator = "user_seq"
)
@Column(
name = "id",
updatable = false
)
val id: Long = -1,
@Column(
name = "first_name",
nullable = false,
length = 50,
updatable = true
)
val firstName: String,
@Column(
name = "last_name",
nullable = false,
length …
Run Code Online (Sandbox Code Playgroud)