Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:08:25+00:00 2026-06-06T15:08:25+00:00

I’m making a simple task list app, and I’m using a database to hold

  • 0

I’m making a simple task list app, and I’m using a database to hold everything. I wrote up a custom CursorAdapter, extending ResourceCursorAdapter, to handle the data. Everything displays fine, but once you start going beyond that, things get sketchy. First, my implementation:

import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Dave Smith
 *
 */
public class TaskCursorAdapter extends ResourceCursorAdapter {

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     * @param flags
     */
    private Context context;
    private int layout, isCheckedFromDb, IdFromDb;
    private Cursor cursor;

    public TaskCursorAdapter(Context context, int layout, Cursor c,
            int flags) {
        super(context, layout, c, flags);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout){
        super(context, layout, null, 0);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout, Cursor c){
        super(context, layout, c, 0);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout, Cursor cursor, boolean autoRequery)
    {
        super(context, layout, cursor, autoRequery);
        this.context = context;
        this.layout = layout;
    }
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);
        return v;
    }
    public void bindView(View v, Context context, Cursor c){
        CheckBox taskCheckBox = (CheckBox)v.findViewById(R.id.taskCheckBox);
        TextView taskText = (TextView)v.findViewById(R.id.taskTitle);
        TextView taskNote = (TextView)v.findViewById(R.id.taskNote);
        final long assignedID = v.getID();
        //v.setClickable(true);     
        //LinearLayout holder = (LinearLayout)v.findViewById(R.id.container);
        //holder.setClickable(true);
        //IdFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.ID));
        taskText.setText(c.getString(c.getColumnIndex(NagTasksDatabaseHelper.TASK)));
        taskNote.setText(c.getString(c.getColumnIndex(NagTasksDatabaseHelper.NOTE)));
        //isCheckedFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.CHECKED));
        taskCheckBox.setChecked(c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.CHECKED))==1);
        //cursor = c;
        taskCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //Really Old method:
                /*NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(null);
                int idCol = cursor.getColumnIndex(NagTasksDatabaseHelper.ID); 
                int taskID = helper.getNewTaskId();
                if (idCol!=-1)
                {
                    taskID= cursor.getInt(idCol);
                    if (isChecked){
                        helper.checkOffTask(taskID);
                    } else {
                        helper.uncheckTask(taskID);
                    }
                }
                //TODO Solve how to use helper to change ISCHECKED in database
                helper.close();
                //Old method:
                NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(null);
                if (isChecked) {
                    helper.checkOffTask(IdFromDb);
                } else {
                    helper.uncheckTask(IdFromDb);
                }
                helper.close();*/
                //Current method:

                if (assignedID!=View.NO_ID) {
                    NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(buttonView.getContext());
                    if (isChecked) {
                        helper.checkOffTask(assignedID);
                    } else {
                        helper.uncheckTask(assignedID);
                    }
                    helper.close();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(buttonView.getContext());
                    builder.setMessage("No ID found. Try something else.");
                    builder.setCancelable(true);
                    builder.create().show();
                }
            } 
        });
    }

}

Issue 1

EDIT: Issue has been resolved, though I do not know how.

In it’s current state, I get an error in Eclipse’s console when I hit run. It doesn’t effect how well it runs at all, but in a previous implementation where I used a layout that only had a CheckBox and just changed the CheckBox’s text, this error didn’t appear:

[2012-06-18 16:10:15 - ddms] null
java.lang.NullPointerException
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

[2012-06-18 16:10:15 - ddms] null
java.lang.NullPointerException
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

Issue 2

The ListFragment that is using this CursorAdapter is not performing its OnListItemClick() method. Similarly, I have registered the ListFragment’s ListView to use a ContextMenu (with registerForContextMenu()), but long-press does not bring up the context menu.

Issue 3

Uncommenting taskCheckBox.setOnCheckedChangeListener generally causes bad things to happen when the checkBox is checked. Typically, it’ll throw a null pointer exception and crash, because it doesn’t know for what _id to call checkOffTask() or uncheckTask(). (EDIT: The changes made in the most recent edit… still don’t do what I want it to do, but at least it doesn’t crash. Also annoying that getItemId() asks for a position and I don’t know how to retrieve that for a given item in a ListView from the cursor, or I’d use that.)


Could anyone help me solve these issues? I’m still mostly unclear of how CursorAdapters work, in spite of asking for help on this subject before.

EDIT: I should note that my app has another list that uses a very lightly tweaked ResourceCursorAdapter (see linked question), and none of these issues have popped up there. (Although issue 3 has no reason to pop up, as it’s a simple list of strings.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:08:27+00:00Added an answer on June 6, 2026 at 3:08 pm

    ISSUE 1:

    Will skip, as it’s been resolved.

    ISSUE 2:

    As noted in this previous question, if a view has a focusable object, it’ll be impossible to make the whole view clickable due to a bug in the SDK. So, I went into the inflated XML and added the attribute android:focusable="false" to each item. List Items are now clickable and long-clickable.

    ISSUE 3:

    As noted by a nice fellow on reddit, I was on the right track with the "Old Method" but needed to use a local final long instead of a parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.