查询以选择sqlite数据库中的行,这些行在今天的日期和7天之间

时间:2015-06-27 13:58:48

标签: android sqlite



public class ListDataAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler{
        TextView id,subject, assignment, dueDate, notes;

    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;

        if(row==null){
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=layoutInflater.inflate(R.layout.row_layout_listview,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.id  =(TextView)row.findViewById(R.id.TEXTid);
            layoutHandler.subject = (TextView)row.findViewById(R.id.textSubject);
            layoutHandler.assignment = (TextView)row.findViewById(R.id.textAssignment);
            layoutHandler.dueDate = (TextView)row.findViewById(R.id.textDueDate);
            layoutHandler.notes = (TextView)row.findViewById(R.id.notesTextField);
            notifyDataSetChanged();
            row.setTag(layoutHandler);

        }
        else{
            layoutHandler = (LayoutHandler)row.getTag();
           notifyDataSetChanged();
        }
        DataProvider dataProvider = (DataProvider)this.getItem(position);
        layoutHandler.id.setText(dataProvider.getAssignmentValue());
        layoutHandler.subject.setText(dataProvider.getSubject());
        layoutHandler.assignment.setText(dataProvider.getAssignment());
        layoutHandler.dueDate.setText(dataProvider.getDueDate());
        layoutHandler.notes.setText(dataProvider.getNotes());
        notifyDataSetChanged();

        return row;
    }
}






public class MainActivity extends Activity {
    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DbHelper dbHelper;
    ListDataAdapter listDataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//
        dbHelper = new DbHelper(getApplicationContext());//
        sqLiteDatabase = dbHelper.getWritableDatabase();//
        listView = (ListView) findViewById(R.id.listView3);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout_listview);
        listView.setAdapter(listDataAdapter);
        
        Cursor cursor = dbHelper.getUpcomingAssignment(sqLiteDatabase);
        if (cursor.moveToFirst()) {
            do {
                final String module, assignment, dueDate, weighting, notes, thisid;
                thisid = cursor.getString(0);
                module = cursor.getString(1);
                assignment = cursor.getString(3);
                dueDate = cursor.getString(4);
                weighting = cursor.getString(2);
                notes = cursor.getString(5);
                DataProvider dataProvider = new DataProvider(weighting, module, assignment, dueDate, notes, thisid);
                listDataAdapter.add(dataProvider);
            }while (cursor.moveToNext());
        }
}






06-27 15:42:39.118    2988-2988/com.ulsteruniversity.eamonn.diary1905 I/art﹕ Late-enabling -Xcheck:jni
06-27 15:42:39.384    2988-2988/com.ulsteruniversity.eamonn.diary1905 E/Database Operations﹕ Database opened/created from constructor.
06-27 15:42:39.386    2988-2988/com.ulsteruniversity.eamonn.diary1905 D/AndroidRuntime﹕ Shutting down VM
06-27 15:42:39.387    2988-2988/com.ulsteruniversity.eamonn.diary1905 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ulsteruniversity.eamonn.diary1905, PID: 2988
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ulsteruniversity.eamonn.diary1905/com.ulsteruniversity.eamonn.diary1905.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
            at com.ulsteruniversity.eamonn.diary1905.DbHelper.getUpcomingAssignment(DbHelper.java:186)
            at com.ulsteruniversity.eamonn.diary1905.MainActivity.onCreate(MainActivity.java:63)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)




我创建了一个小型应用程序来跟踪大学作业。使用sqlite数据库,用户可以存储分配名称,到期日期和分配注释等信息。我创建了一个列表视图,该列表视图将显示即将到来的作业,因此任何作业将在接下来的7天内到期。我似乎无法得到正确的查询,任何可以帮助的人?感谢

//My DbHelper class
public class DbHelper extends SQLiteOpenHelper {

public static final String ASSIGNMENT_ID  = "_id";
public static final String ASSIGNMENT_VALUE = "assignment_value";
public static final String SUBJECT = "subject";
public static final String ASSIGNMENT_TITLE = "assignment_title";
public static final String DUE_DATE = "due_date";
public static final String NOTES_ASSIGNMENT = "notes";
public static final String TABLE_NAME_ASSIGNMENT = "assignment_information";

private static final String DATABASE_NAME = "assignment_info.db";

private static final String ADD_QUERY = "CREATE TABLE "+
        TABLE_NAME_ASSIGNMENT+"("+
        ASSIGNMENT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
        SUBJECT+" TEXT,"+
        ASSIGNMENT_VALUE+" INTEGER,"+
        ASSIGNMENT_TITLE+" TEXT,"+
        DUE_DATE+" DATETIME,"+
        NOTES_ASSIGNMENT+" TEXT);";

    //My query (so far) to return a cursor with the selected assignments
    public Cursor getUpcomingAssignment(SQLiteDatabase sqLiteDatabase){

    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(gc.getTime());

    String sql = "SELECT * FROM " + TABLE_NAME_ASSIGNMENT + " WHERE "+ DUE_DATE + " >= date('now', '+7 days')";

    Cursor mycursor = sqLiteDatabase.rawQuery(sql, null);
    return mycursor;
}



06-27 15:23:54.187  27174-27174/com.ulsteruniversity.eamonn.diary1905 D/AndroidRuntime﹕ Shutting down VM
06-27 15:23:54.188  27174-27174/com.ulsteruniversity.eamonn.diary1905 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ulsteruniversity.eamonn.diary1905, PID: 27174
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ulsteruniversity.eamonn.diary1905/com.ulsteruniversity.eamonn.diary1905.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.ulsteruniversity.eamonn.diary1905.DbHelper.getUpcomingAssignment(android.database.sqlite.SQLiteDatabase)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.ulsteruniversity.eamonn.diary1905.DbHelper.getUpcomingAssignment(android.database.sqlite.SQLiteDatabase)' on a null object reference
            at com.ulsteruniversity.eamonn.diary1905.MainActivity.onCreate(MainActivity.java:64)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)




2 个答案:

答案 0 :(得分:0)

编辑:我发现您的适配器存在许多问题:

  • 好的,现在我看到DataProvider正在做什么。我不是那种模式的粉丝,但我敢打赌你的编译器抱怨因为你有

public class ListDataAdapter extends ArrayAdapter {

而不是

public class ListDataAdapter extends ArrayAdapter<?> {

甚至

public class ListDataAdapter extends ArrayAdapter<Object> {

  • ArrayAdapter已有自己的内部列表,因此您不需要拥有自己的List字段,甚至可以覆盖getView()以外的任何方法,甚至是构造函数。如果您要使用所有这些方法,也可以扩展BaseAdapter。我建议取出List定义,构造函数和除getView()以外的所有覆盖。

  • 您正在notifyDataSetChanged()中呼叫getView()notifyDataSetChanged()仅在列表实际更改时才会被调用。例如,它在super.add()方法中调用,这可能足以触发ListView的刷新。但它在getView()内部没有任何结果,你应该把它拿出来。

从这些更改开始并发布结果。如果您的DataProvider课程没有任何错误,那么您应该处于更好的状态。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.ulsteruniversity.eamonn.diary1905.DbHelper.getUpcomingAssignment(android.database.sqlite.SQLiteDatabase)' on a null object reference

问题不在于您的方法,问题是您尚未初始化运行该方法的DBHelper实例。

答案 1 :(得分:0)

//My query to return a cursor with the selected assignments
public Cursor getUpcomingAssignment(SQLiteDatabase sqLiteDatabase){

    String sql = "SELECT * FROM " + TABLE_NAME_ASSIGNMENT + " WHERE "+ DUE_DATE + " <= date('now','+7 days') AND date('now')";

    Cursor mycursor = sqLiteDatabase.rawQuery(sql, null);

    Log.e("Database Operations","Method getUpComingAssignment called ");

    return mycursor;

}

知道了!