如何使用给定的日期范围过滤android listview

时间:2014-12-19 05:27:30

标签: android listview sorting date filter

我有一个列表视图,其中包含sqlite数据库中的一些数据,如图所示。

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

2 个答案:

答案 0 :(得分:0)

我假设您正在List / ArrayList中保存列表视图数据。在更改这些日期字段时,在列表上应用过滤器,仅保留需要显示的数据。致电后

adapter.notifyDataSetChanged(); 

答案 1 :(得分:-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;
}