0
我一直在試圖理解RxJava 2以及如何使用它。我也試圖通過將我的代碼轉換爲RxJava代碼來了解RxJava。但我似乎還不能理解它。如果有人能夠進一步解釋我如何進行重新編碼,我會非常感謝。轉換代碼到RxJava2代碼
這是我試圖代碼RxJava代碼:
private static User getUserFomCursor(Cursor cursor) {
if (cursor == null || cursor.getCount() == 0) {
return null;
} else {
int myUserIDColumnIndex = cursor.getColumnIndex("_USER_ID");
int usernameColumnIndex = cursor.getColumnIndex("USER_NAME");
int userPasswordColumnIndex = cursor.getColumnIndex("USER_PASSWORD");
int firstNameColumnIndex = cursor.getColumnIndex("FIRST_NAME");
int middleNameColumnIndex = cursor.getColumnIndex("MIDDLE_NAME");
int lastNameColumnIndex = cursor.getColumnIndex("LAST_NAME");
int emailAddressColumnIndex = cursor.getColumnIndex("EMAIL_ADDRESS");
int phoneNumberColumnIndex = cursor.getColumnIndex("PHONE_NUMBER");
int profilePictureColumnIndex = cursor.getColumnIndex("PROFILE_PICTURE");
try {
User user = new User(
cursor.getInt(myUserIDColumnIndex), cursor.getString(usernameColumnIndex),
cursor.getString(userPasswordColumnIndex), cursor.getString(firstNameColumnIndex),
cursor.getString(middleNameColumnIndex), cursor.getString(lastNameColumnIndex),
cursor.getString(emailAddressColumnIndex), cursor.getString(phoneNumberColumnIndex),
cursor.getString(profilePictureColumnIndex));
return user;
} catch (Exception e) {
return null;
}
}
}
public ArrayList<User> getMyUsers() {
User user = new User();
ArrayList <User> userArrayList = new ArrayList<User>();
open();
try {
Cursor cursor = userLocalDatabase.query("User", null, null, null, null, null, null);
while (cursor.moveToNext()) {
user = getUserFomCursor(cursor);
userArrayList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return userArrayList;
}
我試圖讓getMyUsers()方法爲可觀察到的。但是我仍然對如何解決這個問題感到困惑。我真的很感激,如果有人會指出我正確的方向。 :)