英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论