英文:
How to pass two parameters in AsyncTask?
问题
以下是翻译好的部分:
我正在使用MVVM结构,我的Dao查询如下:
@Query("SELECT * FROM Sorted WHERE date LIKE :date AND categoryChart = :category")
LiveData<List<Sorted>> getSortedDiagramData(String date, String category);
在Repository中,我需要在AsyncTask中执行此方法,但我不理解该如何做。
我尝试过以下内容:
public LiveData<List<Sorted>> getSortedDiagramData(String date, String category){
String[] array = new String[2];
array[0] = date;
array[1] = category;
return new GetSortedDiagramDataAsyncTask(sortedDao).execute(array);
}
然后:
private static class GetSortedDiagramDataAsyncTask extends AsyncTask<String[], Void, LiveData<List<Sorted>>> {
private SortedDao sortedDao;
private GetSortedDiagramDataAsyncTask(SortedDao sortedDao){
this.sortedDao = sortedDao;
}
@Override
protected LiveData<List<Sorted>> doInBackground(String[] ... strings) {
String date1 = String.valueOf(strings[0]);
String category1 = String.valueOf(strings[1]);
LiveData<List<Sorted>> list = sortedDao.getSortedDiagramData(date1, category1);
return list;
}
}
但是当我将 "array" 传递给 execute() 时,出现了 "Incompatible types" 错误。
请问您能提供如何解决这个问题的建议吗?非常感谢您的帮助。
英文:
I am using MVVM structure and my query in Dao looks like:
@Query("SELECT * FROM Sorted WHERE date LIKE :date AND categoryChart = :category")
LiveData<List<Sorted>> getSortedDiagramData(String date, String category);
In Repository I need to execute this method in AsyncTask, but I don't understand how to do it.
What I've tried:
public LiveData<List<Sorted>> getSortedDiagramData(String date, String category){
String[] array = new String[2];
array[0] = date;
array[1] = category;
return new GetSortedDiagramDataAsyncTask(sortedDao).execute(array);
}
And then:
private static class GetSortedDiagramDataAsyncTask extends AsyncTask<String[], Void, LiveData<List<Sorted>>> {
private SortedDao sortedDao;
private GetSortedDiagramDataAsyncTask(SortedDao sortedDao){
this.sortedDao = sortedDao;
}
@Override
protected LiveData<List<Sorted>> doInBackground(String[] ... strings) {
String date1 = String.valueOf(strings[0]);
String category1 = String.valueOf(strings[1]);
LiveData<List<Sorted>> list = sortedDao.getSortedDiagramData(date1, category1);
return list;
}
}
But when I pass "array" to execute() there is an error "Incompatible types".
Could you please suggest how I can solve this problem? Thanks for any help.
答案1
得分: 2
你可以在构造函数中传递它:
private String date, category;
private SortedDao sortedDao;
public GetSortedDiagramDataAsyncTask(SortedDao sortedDao, String date, String category) {
this.date = date;
this.category = category;
this.sortedDao = sortedDao;
}
@Override
protected LiveData<List<Sorted>> doInBackground(String[]... strings) {
LiveData<List<Sorted>> list = sortedDao.getSortedDiagramData(date, category);
return list;
}
调用方式如下:
new GetSortedDiagramDataAsyncTask(sortedDao, "date", "category").execute();
英文:
You can pass it in the constructor:
private String date, category;
private SortedDao sortedDao;
public GetSortedDiagramDataAsyncTask(SortedDao sortedDao, String date, String category) {
this.date = date;
this.category = category;
this.sortedDao = sortedDao;
}
@Override
protected LiveData<List<Sorted>> doInBackground(String[]... strings) {
LiveData<List<Sorted>> list = sortedDao.getSortedDiagramData(date, category);
return list;
}
Call it as:
new GetSortedDiagramDataAsyncTask(sortedDao, "date", "category").execute();
答案2
得分: 1
另一种方法是使用以下代码:
GetSortedDiagramDataAsyncTask(sortedDao).execute(date,category);
英文:
Another way would be to use this:
GetSortedDiagramDataAsyncTask(sortedDao).execute(date,category);
专注分享java语言的经验与见解,让所有开发者获益!
评论