2014-12-19 152 views
-2

我有一個列表視圖,其中有一些數據來自sqlite數據庫,如圖所示。如何使用給定的日期範圍篩選android listview

Image

我想通過選擇上面的文本視圖中的兩個日期範圍過濾此列表。如何使用這些文本視圖過濾顯示的列表?請幫助..

數據庫類

public class AndroidSQLiteData extends SQLiteOpenHelper 
{ 

    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    public static final String DATABASE_NAME = "expensedata"; 

    // Labels table name 
    public static final String TABLE_NAME = "expense"; 

    // Labels Table Columns names 
    public static final String KEY_ID = "id"; 
    public static final String KEY_CATEGORY = "category"; 
    public static final String KEY_MONEY="moneytype"; 
    public static final String KEY_DATE="date"; 
    public static final String KEY_AMOUNT="amount"; 
    public static final String KEY_DESCRIPTION="description"; 


    private SQLiteDatabase db=null; 

    public AndroidSQLiteData(Context context) 
    { 
     super(context,DATABASE_NAME,null,DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sdb) 
    { 
     String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_CATEGORY + " TEXT,"+KEY_DATE+ " TEXT," +KEY_MONEY+" TEXT," +KEY_AMOUNT+ " TEXT," +KEY_DESCRIPTION+ " TEXT)"; 
    sdb.execSQL(CREATE_CATEGORIES_TABLE); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db);  

    } 

    public void insertData(String category, String date, String amount, String moneytype, String description) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_CATEGORY, category); 
     values.put(KEY_DATE, date); 
     values.put(KEY_MONEY, moneytype); 
     values.put(KEY_AMOUNT, amount); 
     values.put(KEY_DESCRIPTION, description); 
     // Inserting Row 
     db.insert(TABLE_NAME, null, values); 
     db.close(); 
     // Closing database connection 

    } 

    public List<String> getAllData(){ 
     List<String> data = new ArrayList<String>(); 

     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_NAME; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) 

     { 
      do 
      {    
       data.add(cursor.getString(1)); 
      } 

      while (cursor.moveToNext()); 
     } 

     // closing connection 
     cursor.close(); 
     db.close(); 

     return data; 
    } 

    public void open() 
    { 
     if(this.db==null) 
     { 
      this.db=this.getWritableDatabase(); 
     } 
    } 
} 
+0

您必須更新適配器中的數據。如果使用遊標適配器,請發送新的查詢以查找日期範圍內的結果,然後將其分配給適配器 – Panther 2014-12-19 05:31:06

回答

-1

您可以在databasehelper類使用查詢象下面這樣:

Cursor mCur = db.rawQuery("SELECT * FROM expense WHERE yourdatefield BETWEEN ? AND ?", new String[]{startdate, enddate}); 

使用這種查詢就像在你的代碼如下:

public List<String> getAllData(String startdate, String enddate){ 
    List<String> data = new ArrayList<String>(); 


    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor mCur = db.rawQuery("SELECT * FROM date WHERE date BETWEEN ? AND ?", new String[]{startdate, enddate}); 

    // looping through all rows and adding to list 
    if (mCur.moveToFirst()) 

    { 
     do 
     {    
      data.add(mCur.getString(1)); 
      ..... as per your need 
     } 

     while (mCur.moveToNext()); 
    } 

    // closing connection 
    mCur.close(); 
    db.close(); 

    return data; 
} 
+0

如何在過濾器類中使用此遊標? – Reshmin 2014-12-19 05:50:20

+0

顯示您的數據庫類 – Riser 2014-12-19 05:57:07

+0

編輯問題與數據庫類.. – Reshmin 2014-12-19 06:07:11

0

我認爲你正在列表視圖數據在一個L IST/ArrayList中。在更改這些日期字段時,在列表上應用過濾器,只保留需要顯示的數據。撥打電話後

adapter.notifyDataSetChanged(); 
+0

程序員不**假設**,他們**假設** .. – Elltz 2014-12-19 05:52:53

+0

謝謝@Elltz,糾正 – Shiv 2014-12-19 06:34:53