Where call cursor in Adapter Class

57 Views Asked by At

I have the class GetAllENtrysListViewAdapter extends BaseAdapter where the list View in the PinboardActivity is set with an array. I want to change the likeImage if there is saved an certain entry in the SQLite Database. I call the getLike Cursor in getView this way:

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();
        cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeImage = (ImageView) convertView.findViewById(R.id.heartImage);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        cell.likes.setText(jsonObject.getString("likes"));
        cell.note.setText(jsonObject.getString("note"));

        String img = jsonObject.getString("image");
        String urlForImageInServer = baseUrlForImage + img;

        new AsyncTask<String, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(String... params) {
                //download image
                String url = params[0];
                Bitmap icon = null;

                try {
                    InputStream in = new java.net.URL(url).openStream();
                    icon = BitmapFactory.decodeStream(in);
                } catch(MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return icon;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                cell.img.setImageBitmap(result);
            }

        }.execute(urlForImageInServer);

//CURSOR IS CALLED --> THIS LINE CAUSES THE ERROR
        cursor = dbh.getLike(dbh);
        cursor.moveToFirst();
        String markerID = pinboard.markerID;
        if (cursor.moveToFirst()) {
            do {
                if (cursor.getString(0).equals(markerID) && Integer.parseInt(cursor.getString(1))== position && Integer.parseInt(cursor.getString(2)) == 1) {
                    cell.likeImage.setImageResource(R.drawable.heart_filled);
                }
            }
            while(cursor.moveToNext());
        }
        cursor.close();

    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}

public static class ListCell {
    private TextView likes;
    private TextView note;
    private ImageView img;
    public ImageView likeImage;
    public boolean touched;

}

As opening the PinboardActivity I get the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor de.mareike.cityexplorer.DbHelper.getLike(de.mareike.cityexplorer.DbHelper)' on a null object reference

Am I calling the cursor on the wrong place or what could cause the issue? By the way, the context is set this way:

 public GetAllEntrysListViewAdapter(Context context, Cursor cursor) {
    super();
    cursor = cursor;
    DbHelper dbh  = new DbHelper(context);
   inflater=LayoutInflater.from(context);
}
0

There are 0 best solutions below