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 7711525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:23:51+00:00 2026-06-01T01:23:51+00:00

Working on figuring out my NullPointerException within my custom simplecursoradapter when trying to refresh

  • 0

Working on figuring out my NullPointerException within my custom simplecursoradapter when trying to refresh the data in my view. When using SimpleCursorAdapter you cannot use NotifyDataSetChanged() so I need to create a new adapter and I am having difficulty passing on the data required.

public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
DxDbAdapter dbh;
DxSimpleCursorAdapter adapter;
ListView lv;

protected String subcategory;

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, String param) {
    super(context, layout, c, from, to);
    this.context=context;
    this.activity=(Activity) context;
    subcategory = param;
}

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context=context;
    this.activity=(Activity) context;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(R.layout.list_detail, null);
    ViewHolder holder = new ViewHolder();
    holder.image = (ImageView) row.findViewById(R.id.fav);
    holder.diagnosis = (TextView) row.findViewById(R.id.diagnosis);
    holder.code = (TextView) row.findViewById(R.id.code);
    row.setTag(holder);
    return row;
}

class ViewHolder {
    ImageView image;
    TextView diagnosis;
    TextView code;      
}

class Status {
    int status;
    Long id;
}

@Override
public void bindView(View v, Context con, Cursor cursor) {
    ViewHolder holder = (ViewHolder) v.getTag();

    int favstatus = cursor.getInt(cursor.getColumnIndex(DxDbAdapter.FAV));
    Status state = new Status();

    if (favstatus == 1) {
        holder.image.setImageResource(R.drawable.btn_star_on_normal);
        state.status = 1;
    }
    else if (favstatus == 0) {
        holder.image.setImageResource(R.drawable.btn_star_off_normal);
        state.status = 0;
    }

    long id = cursor.getLong(cursor.getColumnIndex(DxDbAdapter.DIAG_ID));
    state.id = id;
    holder.image.setTag(state);
    holder.diagnosis.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG)));
    holder.code.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE)));

    holder.image.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Status current_state = (Status) v.getTag();

            ImageView fav = (ImageView) v.findViewById(R.id.fav);
            fav.setImageResource(R.drawable.ic_fav);

            if (current_state.status == 1) {
                Toast toast = Toast.makeText(context,"Update fav status to 1",Toast.LENGTH_SHORT);
                toast.show();
            }
            else if (current_state.status == 0) {
                Toast toast = Toast.makeText(context,"Update fav status to 0",Toast.LENGTH_SHORT);
                toast.show();
            }

                    dbh = new DxDbAdapter(context);
            dbh.open();
            Cursor newCursor = dbh.fetch(1, subcategory);
            String[] columns = new String[] {"diagnosis", "diagcode"};
            int[] to = new int[] {R.id.diagnosis, R.id.code};
            adapter = new DxSimpleCursorAdapter(context, R.layout.list_detail, newCursor, columns, to);
            lv.setAdapter(adapter);
        }
    });
    String diag = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG));
    String code = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE));
    holder.diagnosis.setText(diag);
    holder.code.setText(code);
}

The issue is with subcategory = getIntent().getStringExtra(“SUBCATEGORY”); and Cursor newCursor = dbh.fetch(1, subcategory); and that I have to reference the Activity that is using the adapter. I guess I am having issues on the correct syntax to reference the Activity so that I don’t get a NullPointerException.

  • 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-01T01:23:53+00:00Added an answer on June 1, 2026 at 1:23 am

    You should really pass in the subcategory from your parent activity since this value will not change while using your adapter (instead of retrieving it every single time you bind to a row)

    Ex: from the parent activity you would do something like

    String subcategory = getIntent().getStringExtra("SUBCATEGORY"); 
    DxSimpleCursorAdapter adapter = DxSimpleCursorAdapter(context, layout, c, from, to, subcategory);
    listView.setAdapter(adapter); 
    

    Then obviously you would need to modify the constructor of your adapter so it took that string and then assigned it to your subcategory variable within the adapter

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

Sidebar

Related Questions

I'm having trouble figuring out why the following is not working: My custom JS
I'm working on my first Core Data-backed app and am having trouble figuring out
I'm working at figuring out how to best work within my own repo for
I've been working on figuring out how to receive HTTP Headers via a request
I am stuck figuring out a working SQL Query for the fallowing: I need
I'm having trouble figuring out why my web service is not working correctly when
I need help figuring out how to change out the view in my application.
Having some trouble figuring out why my ImageView buttons stop working after I change
I am working on a REST based API, and having some trouble figuring out
I have been working on figuring out how to make a double drop down

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.