在适配器类中调用游标的位置

时间:2015-06-19 16:28:27

标签: java android sqlite cursor baseadapter

我有一个类GetAllENtrysListViewAdapter extends BaseAdapter,其中PinboardActivity中的列表视图设置了一个数组。 如果在SQLite数据库中保存了某个条目,我想更改likeImage。我用getLike这样调用getView光标:

 @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;

}

打开PinboardActivity时出现以下错误: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

我是否将光标调到错误的位置或导致问题的原因? 顺便说一句,上下文是这样设置的:

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

0 个答案:

没有答案