Search code examples
androidlistviewsimplecursoradapterandroid-cursoradapter

android newView/bindView of custom cursorAdapter not calling


It was working fine, I dont know what changes I made, what I had done wrong, it is not working anymore

everything is ok, table is creating, values are inserting, even displaying table data in log, call is going to custom cursor adapter, but all i came to know through debugging is

newView and Bind view methods of my custom cursor adapter not calling

  • During debugging I saw values are there in cursor when in custom cursorAdapter constructor but newView and bindView is not calling here is mmy code:

    public class DbHelper extends SQLiteOpenHelper{
    
    private static final String dbName = "(AnnoyingAlarmDatabase).db";
    private static final int version = 1;
    public static final String keyId = "_id";
    public static final String hour = "hour";
    public static final String minute = "min";
    public static final String strength = "strength";
    public static final String vibrate = "vibrate";
    public static final String snooze = "snooze";
    public static final String method = "method";
    public static final String label = "label";
    public static final String days = "days";
    
    private static final String createTable = "create table alarms (" + keyId + " integer primary key, " + label + " text, " + hour + " integer, " + minute + " integer, " + strength + " text, " + vibrate + " text, " + snooze + " integer, " + method + " text " +")";
    
    
    public DbHelper(Context context) {
    // TODO Auto-generated constructor stub
    super(context, dbName, null, version);
    Log.d("mydb", "constructor");
    }
    
      @Override
    
          public void onCreate(SQLiteDatabase db) {
    try{
    
    Log.d("mydb", "oNCreate()");
    db.execSQL(createTable);
    }
    
    catch( SQLException e)
    {
        e.printStackTrace();
    }
     }
    
     @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS alarms" );
    
    
        onCreate(db);
    
     }
    
          public void addAlarm( String getLabel, int getHour, int getMin, String getStrength, String getVibrate, int getSnooze, String getMethod, String getDays )
      {
    
               Log.d("mydb", "addAlarm");
       SQLiteDatabase db = this.getWritableDatabase();
       ContentValues values = new ContentValues();
    
       values.put( label , getLabel);
       values.put( hour , getHour);
    values.put( minute , getMin);
    values.put( strength , getStrength);
    values.put( vibrate , getVibrate);
    values.put( snooze , getSnooze);
    values.put( method , getMethod);
    
    db.insert("alarms", null, values);
    
    db.close();
    
    
       }
    
          public Cursor getAlarm( )
      {
    
              Log.d("mydb", "getAlarm");
    
    
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from alarms", null);
    
    try{
        while( cursor.moveToNext())
        {
            Log.d("db id", cursor.getString(0));
            Log.d("db label",cursor.getString(1));
            Log.d("db hour",cursor.getString(2));
            Log.d("db minute",cursor.getString(3));
            Log.d("db strength",cursor.getString(4));
            Log.d("db vibrate",cursor.getString(5));
            Log.d("db snooze",cursor.getString(6));
            Log.d("db method",cursor.getString(7));
            //Log.d("db days",cursor.getString(8));
        }
    
    }
    
    catch( SQLException e )
    {
        e.printStackTrace();
    }
    
    finally{
    //  cursor.close();
        db.close();
    }
    
    cursor.moveToFirst();
    
    //db.close();
      return cursor;
        }
    

activity:

     public class ViewAlarms extends ListActivity {

Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    DbHelper db = new DbHelper(this);

    cursor = db.getAlarm();
String [] from = {DbHelper.label};
    int [] to = {R.id.textViewLabel};


//  SimpleCursorAdapter adapter =  new SimpleCursorAdapter(this, R.layout.row_view, cursor, from, to);
//  setListAdapter(adapter);


    CustomCursorAdapter adapter = new CustomCursorAdapter(this, cursor);
    setListAdapter(adapter);
    cursor.close();

}

custom cursor adapter

public class CustomCursorAdapter extends CursorAdapter {

    private LayoutInflater inflator;
    private int hourIndex;
    private int minIndex;
    private int labelIndex;


    public CustomCursorAdapter(Context context, Cursor c) {
        super(context, c);
        inflator = LayoutInflater.from(context);
        hourIndex = c.getColumnIndex(DbHelper.hour);
        minIndex = c.getColumnIndex(DbHelper.minute);
        labelIndex = c.getColumnIndex(DbHelper.label);
        Log.d("customAdapter", "constructor");
        //Log.d("adapter curconst",c.getString(minIndex));
        }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView label = (TextView)view.findViewById(R.id.textViewLabel);
        TextView time = (TextView)view.findViewById(R.id.textViewTIme);
        Log.d("adapter cursor","chala?");

    //time
        int hour = Integer.parseInt(cursor.getString(hourIndex));
        int min = Integer.parseInt(cursor.getString(minIndex));
        String minStr;

        if ( min < 10 )
        {
            minStr = "0" + min;
        }
        else
        {
            minStr = "" + min;
        }
        if ( hour < 12 )
        {
            if ( hour == 0 )
                hour = 12;
            time.setText(hour + ":" + minStr + " Am");
        }

        if ( hour > 12 )
        {
            if ( hour == 0 )
                hour = 12;
            time.setText(hour % 12 + ":" + minStr + " Pm");
        }

        //label
        label.setText(cursor.getString(labelIndex));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup arg2) {
        Log.d("newView","chala");
        return inflator.inflate(R.layout.row_view, arg2, false);
    }

}

Even i tried simple cursor adapter,it still does not show anything.


Solution

  • The constructor you are using has been deprecated . You should be using a Loader. Here is some more documentation

    To try to answer your question. Have you tried removed the cursor.close() line right after your call to setAdapter(). You may be closing your cursor while your adapter still needs it. This would also be fixed by using a Loader because that handels lifecycle events better for you.