如何在AsyncTask中传递两个参数?

huangapple go评论70阅读模式
英文:

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(&quot;SELECT * FROM Sorted WHERE date LIKE :date AND categoryChart = :category&quot;)
LiveData&lt;List&lt;Sorted&gt;&gt; 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&lt;List&lt;Sorted&gt;&gt; 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&lt;String[], Void, LiveData&lt;List&lt;Sorted&gt;&gt;&gt; {
    private SortedDao sortedDao;
    private GetSortedDiagramDataAsyncTask(SortedDao sortedDao){
        this.sortedDao = sortedDao;
    }
    @Override
    protected LiveData&lt;List&lt;Sorted&gt;&gt; doInBackground(String[] ... strings) {
        String date1 = String.valueOf(strings[0]);
        String category1 = String.valueOf(strings[1]);
        LiveData&lt;List&lt;Sorted&gt;&gt; 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&lt;List&lt;Sorted&gt;&gt; doInBackground(String[]... strings) {
    LiveData&lt;List&lt;Sorted&gt;&gt; list = sortedDao.getSortedDiagramData(date, category);
    return list;
}

Call it as:

new GetSortedDiagramDataAsyncTask(sortedDao, &quot;date&quot;, &quot;category&quot;).execute();

答案2

得分: 1

另一种方法是使用以下代码:

GetSortedDiagramDataAsyncTask(sortedDao).execute(date,category);
英文:

Another way would be to use this:

GetSortedDiagramDataAsyncTask(sortedDao).execute(date,category);

huangapple
  • 本文由 发表于 2020年5月4日 21:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61593862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定