排序列表视图如何通过上次修改填充sqlite?

时间:2016-05-16 08:03:29

标签: android sqlite sorting listview

我是绝对的初学者。 我有一个填充了sqlite表的listview,我有两个问题:

1-如何按最后修改的项目对此列表视图进行排序?

2-如何在我的第一页上打开一个按钮,打开上次修改的项目而不进入listview!

以下是我的代码:

其列表视图 -

public class MainActivity extends ListActivity {

// Declare Variables
public static final String  ROW_ID = "row_id";
private static final String TITLE  = "title";
private ListView            noteListView;
private CursorAdapter       noteAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    stopService(new Intent(MainActivity.this, MyService.class));

    // Locate ListView
    noteListView = getListView();

    // Prepare ListView Item Click Listener
    noteListView.setOnItemClickListener(viewNoteListener);

    // Map all the titles into the ViewTitleNotes TextView
    String[] from = new String[]{ TITLE };
    int[] to = new int[]{ R.id.ViewTitleNotes };

    // Create a SimpleCursorAdapter
    noteAdapter = new SimpleCursorAdapter(MainActivity.this,
            R.layout.list_zekr, null, from, to);

    // Set the Adapter into SimpleCursorAdapter
    setListAdapter(noteAdapter);
}

// Capture ListView item click
OnItemClickListener viewNoteListener = new OnItemClickListener() {

                                         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                                             // Open ViewNote activity
                                             Intent viewnote = new Intent(MainActivity.this, CounterActivity.class);
                                             viewnote.putExtra(ROW_ID, arg3);
                                             startActivity(viewnote);
                                         }
                                     };


@Override
protected void onResume() {
    super.onResume();

    // Execute GetNotes Asynctask on return to MainActivity
    new GetNotes().execute((Object[]) null);
}


@Override
protected void onStop() {
    Cursor cursor = noteAdapter.getCursor();

    // Deactivates the Cursor
    if (cursor != null)
        cursor.deactivate();

    noteAdapter.changeCursor(null);
    super.onStop();
}


// Create an options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Menu Title
    menu.add("ذکر جدید")
            .setOnMenuItemClickListener(this.AddNewNoteClickListener)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return super.onCreateOptionsMenu(menu);
}

// Capture menu item click
OnMenuItemClickListener AddNewNoteClickListener = new OnMenuItemClickListener() {

                                                    public boolean onMenuItemClick(MenuItem item) {

                                                        // Open AddEditNotes activity
                                                        Intent addnote = new Intent(MainActivity.this, AddEditNotes.class);
                                                        startActivity(addnote);

                                                        return false;

                                                    }
                                                };


// GetNotes AsyncTask
public class GetNotes extends AsyncTask<Object, Object, Cursor> {

    DatabaseConnector dbConnector = new DatabaseConnector(MainActivity.this);


    @Override
    protected Cursor doInBackground(Object... params) {
        // Open the database
        dbConnector.open();

        return dbConnector.ListAllNotes();
    }


    @Override
    protected void onPostExecute(Cursor result) {
        noteAdapter.changeCursor(result);

        // Close Database
        dbConnector.close();
    }

}

}

我的DatabaseConnector:

public class DatabaseConnector {
// Declare Variables
private static final String DB_NAME          = "database";
private static final String TABLE_NAME       = "tablenotes";
private static final String TITLE            = "title";
private static final String ID               = "_id";
private static final String NOTE             = "note";
private static final String COUNTS           = "counts";
private static final String LIMITS           = "limits";
private static final int    DATABASE_VERSION = 2;
private SQLiteDatabase      database;
private DatabaseHelper      dbOpenHelper;


public DatabaseConnector(Context context) {
    dbOpenHelper = new DatabaseHelper(context, DB_NAME, null, DATABASE_VERSION);

}


// Open Database function
public void open() throws SQLException {
    // Allow database to be in writable mode
    database = dbOpenHelper.getWritableDatabase();
}


// Close Database function
public void close() {
    if (database != null)
        database.close();
}


// Create Database function
public void InsertNote(String title, String note, String counts, String limits) {
    ContentValues newCon = new ContentValues();
    newCon.put(TITLE, title);
    newCon.put(NOTE, note);
    newCon.put(COUNTS, counts);
    newCon.put(LIMITS, limits);

    open();
    database.insert(TABLE_NAME, null, newCon);
    close();
}


// Update Database function
public void UpdateNote(long id, String title, String note, String counts, String limits) {
    ContentValues editCon = new ContentValues();
    editCon.put(TITLE, title);
    editCon.put(NOTE, note);
    editCon.put(COUNTS, counts);
    editCon.put(LIMITS, limits);

    open();
    database.update(TABLE_NAME, editCon, ID + "=" + id, null);
    close();
}


// Delete Database function
public void DeleteNote(long id) {
    open();
    database.delete(TABLE_NAME, ID + "=" + id, null);
    close();
}


// List all data function
public Cursor ListAllNotes() {
    return database.query(TABLE_NAME, new String[]{ ID, TITLE }, null,
            null, null, null, TITLE);
}


// Capture single data by ID
public Cursor GetOneNote(long id) {
    return database.query(TABLE_NAME, null, ID + "=" + id, null, null,
            null, null);
}

}

提前感谢。

更新!

好的,我在表格中创建了一个名为“时间”的列 我可以像这样插入INTEGER的时间:20160516100740

所以现在每一排表都有这样的时间,现在我能做什么?

更新!

好的,我在列表活动中写了这个(MainActivity.java) 但它不起作用:(

public Cursor listAllSortedNotes() {
    String selectQuery = "SELECT * FROM " + TABLE_NAME + " ORDER BY time DESC";
    return database.rawQuery(selectQuery, null);

1 个答案:

答案 0 :(得分:0)

您需要在数据库中添加一个应该是日期字符串的字段(示例:yyyy-MM-dd HH:mm:ss)。更新数据时,请使用当前日期和时间进行更新。然后,您可以使用选择查询来使用

获取最近的更新数据
SELECT * 
FROM Table 
ORDER BY datetime (dateColumn) DESC 

在你的情况下,你可以做这样的事情。

public Cursor listAllSortedNotes() {
    String selectQuery = "SELECT * FROM "+ TABLE_NAME + " ORDER BY datetime(dateColumn) DESC";
    return database.rawQuery(selectQuery, null);
}