所以我的代码是:
private class getRankedTeamsTask extends AsyncTask<ApiConnector,Long,JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].getRankedTeams();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
try {
JSONObject json = null;
if (jsonArray != null) {
json = jsonArray.getJSONObject(0);
String[] columns = new String[6];
for(int i = 0; i<json.length(); i++){
columns[i] = json.names().getString(i);
}
MatrixCursor matrixCursor = new MatrixCursor(columns);
startManagingCursor(matrixCursor);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
matrixCursor.addRow(new Object[] {jo.getString("teamName"), jo.getString("loses"), jo.getString("points"),
jo.getString("ranking"), jo.getString("wins")});
};
int[] toViewIDs = new int[]
{R.id.teamNameLV, R.id.losesLV, R.id.pointsLV, R.id.rankLV, R.id.winsLV};
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
this,
R.layout.custom_ranking,
matrixCursor,
columns,
toViewIDs
);
rankingList.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
代码从数据库获取数据,我正在尝试将数据添加到自定义动态ListView
但我得到一些奇怪的错误,不让应用程序运行。有人能帮我吗?我看到很多关于这个的帖子和教程,我只是这样找到了,但是我得到了错误而他们没有。
错误:
Error:(92, 31) error: no suitable constructor found for SimpleCursorAdapter(RankingActivity.getRankedTeamsTask,int,MatrixCursor,String[],int[])
constructor SimpleCursorAdapter.SimpleCursorAdapter(Context,int,Cursor,String[],int[],int) is not applicable
(actual and formal argument lists differ in length)
constructor SimpleCursorAdapter.SimpleCursorAdapter(Context,int,Cursor,String[],int[]) is not applicable
(actual argument RankingActivity.getRankedTeamsTask cannot be converted to Context by method invocation conversion)
答案 0 :(得分:0)
在this
构造函数中使用SimpleCursorAdapter
,您将引用封闭的类,即getRankedTeamsTask
:
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
this,
R.layout.custom_ranking,
matrixCursor,
columns,
toViewIDs
);
但是,第一个参数应该是Context
实例,这是您的活动,因此您应该将其替换为以下内容:
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
RankingActivity.this,
R.layout.custom_ranking,
matrixCursor,
columns,
toViewIDs
);