【问题标题】:How to Filter android listview using a given date range如何使用给定的日期范围过滤 android listview
【发布时间】:2015-02-18 01:03:38
【问题描述】:

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

我想通过选择具有上述文本视图的两个日期范围来过滤此列表。如何使用这些文本视图过滤显示的列表?请帮忙..

数据库类

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

【问题讨论】:

  • 您必须更新适配器中的数据。如果您使用游标适配器,请发送新查询以查找日期范围之间的结果,然后将其分配给适配器

标签: android listview sorting date filter


【解决方案1】:

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

adapter.notifyDataSetChanged(); 

【讨论】:

    【解决方案2】:

    您可以在数据库助手类中使用如下查询:

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

    【讨论】:

    • 如何在过滤器类中使用这个游标?
    • 将此添加到数据库类中。但没有从过滤器类中获取值.. List newData=sdbData.getDate(startdate, enddate);在此之后如何检查两个文本视图日期并过滤列表视图?
    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 2016-06-14
    • 2020-02-26
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多