所以我正在使用Android Room库在SQLite数据库上执行请求。
我无法在UI线程上发出请求,因此我正在使用AsyncTask。
我的应用程序很简单,我只是希望能够将新用户插入我的数据库。
所以有我的UserDao.java:
@Dao
public interface UserDao {
@Insert
long insert(User user);
@Update
void update(User user);
@Delete
void delete(User user);
}
还有我的User.java:
@Entity
public class User {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private int age;
// Getter / Setter
}
然后,要执行我的插入请求,我必须制作一个完整的AsyncTask类:
public static class insertAsyncTask extends AsyncTask<User, Void, User> {
private WeakReference<Activity> weakActivity;
private UserDao dao;
// Constructeur
insertAsyncTask(Activity activity, UserDao dao) {
this.weakActivity = new WeakReference<>(activity);
this.dao = dao;
}
@Override
protected User doInBackground(final User... params) {
return this.dao.getById(this.dao.insert(params[0]));
}
@Override
protected void onPostExecute(User u) {
// Some code
}
最后,我这样称呼“插入”:
new AppDatabase.insertAsyncTask(this, db.userDao()).execute(myUser);
那么是否可以使我的AsyncTask类更通用? 我希望能够将此类用于多个请求(例如插入,更新,删除...)。
还是应该改用通用DAO?
在此先感谢我的英语;)
答案 0 :(得分:0)
是的,您可以先在任何地方使用它,然后为所有方法创建通用的Async任务类 您可以创建一个接口,将其传递给AsyncTask(在构造函数中),然后在onPostExecute()中调用方法
For example:
1。您的界面:
public interface OnTaskRoomCompleted{
void onTaskCompleted(User);
}
2。您的活动:
public class YourActivity implements OnTaskRoomCompleted{
// your Activity
}
3。还有您的AsyncTask:
public class YourTask extends AsyncTask<User,Object,User>{ //change Object to required type
private OnTaskRoomCompleted listener;
public YourRoomTask(OnTaskCompleted listener){
this.listener=listener;
}
// required methods
protected void onPostExecute(Object user){
// your stuff
listener.onTaskRoomCompleted(user);
}
}
答案 1 :(得分:0)
@Override public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState){ super.onViewCreated(view,savedInstanceState);
UserId = view.findViewById(R.id.user_id);
UserName= view.findViewById(R.id.user_name_id);
UserEmail=view.findViewById(R.id.user_email_id);
save = view.findViewById(R.id.bn_save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = UserId.getText().toString();
String name = UserName.getText().toString();
String email =UserEmail.getText().toString();
String userData[] ={id,name,email};
GetUsersAsyncTask task = new GetUsersAsyncTask();
try {
String status = task.execute(userData).get();
Toast.makeText(getContext(),status , Toast.LENGTH_SHORT).show();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
//emty the fields of the add user in the UI
UserId.setText("");
UserName.setText("");
UserEmail.setText("");
}
});
}
private static class GetUsersAsyncTask extends AsyncTask<String[], Void,String> {
@Override
protected String doInBackground(String[]... strings) {
String[] userDatas = strings[0];
int id = Integer.parseInt(userDatas[0]);
String name = userDatas[1];
String email =userDatas[2];
User user = new User();
user.setId(id);
user.setName(name);
user.setEmail(email);
MainActivity.myAppDatabase.myDao().addUser(user);
return "Data Inserted SuccessFully....";
}
}