Simplest error SimpleCursorAdapter

So my code is:

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();
            }
        }
    }

      

The code is getting data from the database and I am trying to add data to the custom dynamic ListView

, but I am getting some strange errors that prevent the application from starting. Can anyone help me? I have seen many posts and tutorials about this and I only found this way, but I get an error and they donโ€™t.

mistake:

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)

      

+3


source to share


1 answer


By using this

in the constructor SimpleCursorAdapter

, you are referring to the include class, which getRankedTeamsTask

:

 SimpleCursorAdapter adapter
     = new SimpleCursorAdapter(
         this,
         R.layout.custom_ranking,
         matrixCursor,
         columns,
         toViewIDs
     );

      

However, the first argument must be an instance Context

, which is your activity, so you must replace it with the following:



SimpleCursorAdapter adapter
    = new SimpleCursorAdapter(
        RankingActivity.this,
        R.layout.custom_ranking,
        matrixCursor,
        columns,
        toViewIDs
    );

      

Please note, this try- to-use constructor is deprecated on a per-document basis, you should use this instead.

0


source







All Articles