android newView / bindView自定义cursorAdapter没有调用

时间:2012-08-04 21:05:12

标签: android listview simplecursoradapter android-cursoradapter

工作正常,我不知道我做了什么改变,我做错了什么,它不再工作了

一切正常,表正在创建,值正在插入,甚至在日志中显示表数据,调用是自定义游标适配器,但我通过调试知道的是

我的自定义游标适配器的newView和Bind视图方法未调用

  • 在调试过程中,我在自定义cursorAdapter构造函数中看到了光标中的值但newView和bindView没有调用 这是mmy代码:

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

活性:

     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();

}

自定义游标适配器

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);
    }

}

即使我尝试过简单的游标适配器,它仍然没有显示任何内容。

1 个答案:

答案 0 :(得分:4)

您使用的构造函数已被弃用。你应该使用一个装载机。 Here is some more documentation

试着回答你的问题。您是否尝试在调用setAdapter()后立即删除了cursor.close()行。当适配器仍然需要它时,您可能正在关闭光标。这也可以通过使用Loader来解决,因为这样可以更好地处理生命周期事件。