如何获取具有最大数值的Sqlite表的行ID&在Activity中打开它?

时间:2016-05-17 09:40:34

标签: android sqlite

我是绝对的初学者。

我有一个Sqlite表,它有5列,其中一个是“时间”。

当用户查看我的每个Sqlite表行时,“时间”列会更新当前时间,因此始终最后看到的行具有最大数字(“时间”)。

在我的第一个活动中,我想创建一个按钮,用最大的“时间”数字打开最后一行。

在用户可以看到行值的Activity中的

是一些填充Sqlite行数据的TextView,并在用户关闭活动时更新它们。当用户单击listview项目之一时,Row值将putextra发送到Activity ...

问题是:我不能从第一个活动中增加额外活动因为listview活动中有很多工作人员,我不知道该怎么办!!!

这是我的Listview代码:

public class MainActivity extends ListActivity {

// Declare Variables
public static final String  ROW_ID     = "row_id";
private static final String TITLE      = "title";
private static final String COUNTS     = "counts";
private static final String TIME       = "time";
private static final String TABLE_NAME = "tablenotes";

private ListView            noteListView;
private CursorAdapter       noteAdapter;
private SQLiteDatabase      database;


@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);
                                             finish(); //  test
                                         }
                                     };


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

这是我的第一页代码:

   btnlastz.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
   Intent viewnote = new Intent(Firstpage.this, CounterActivity.class);

      //  what should i write here to open last ROW in sqlite?
      //                    startActivity(viewnote);

      }

    });

这是我的Activity的一部分,显示了sqlite值:

 Bundle extras = getIntent().getExtras();
    rowID = extras.getLong(MainActivity.ROW_ID);
private class LoadNotes extends AsyncTask<Long, Object, Cursor> {

    // Calls DatabaseConnector.java class
    DatabaseConnector dbConnector = new DatabaseConnector(CounterActivity.this); // Instance of DatabaseConnector Activity for make methods work! ahmad


    @Override
    protected Cursor doInBackground(Long... params) {
        // Pass the Row ID into GetOneNote function in
        // DatabaseConnector.java class
        dbConnector.open();
        return dbConnector.GetOneNote(params[0]);
    }


    @Override
    protected void onPostExecute(Cursor result) {
        super.onPostExecute(result);
        result.moveToFirst();
        // Retrieve the column index for each data item
        int TitleIndex = result.getColumnIndex(TITLE);
        int NoteIndex = result.getColumnIndex(NOTE);
        int CountsIndex = result.getColumnIndex(COUNTS);
        //            Log.i("Log", "onPostExecute - C is: " + C); ahmad
        int LimitsIndex = result.getColumnIndex(LIMITS);

        // Set the Text in TextView
        title_edit.setText(result.getString(TitleIndex));
        note_edit.setText(result.getString(NoteIndex));
        counts_edit.setText(result.getString(CountsIndex));
        limits_edit.setText(result.getString(LimitsIndex));

        result.close();
        dbConnector.close();

    }
}

如果您需要更多数据,请询问。

提前感谢。

2 个答案:

答案 0 :(得分:0)

public static final String ID = "id";
public static final String ROW_TITLE = "row_title";
public static final String ROW_COUNTS = "row_counts";
public static final String ROW_TIME = "row_time";
public static final String ROW_TABLE = "row_table";

 private String lastRowId,lastRowTitle, lastRowCount,
        lastRowTime, lastRowTableName;   

//above are global variable
public void onClick(View arg0) {
   /**
     * projection = DB projection is any else = null
     * sort = DB sort order is any else = null
     */

    /**
     * get all last row item
     * uri is your DB ContentUri
     */
    try {

        Cursor cursor = getContentResolver()
                .query(uri, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
            do {

                /**
                 * This will return the last item of the DB
                 */
                lastRowId = cursor.getString(cursor.getColumnIndex(ROW_ID));
                lastRowTitle = cursor.getString(cursor.getColumnIndex(TITLE));
                lastRowCount = cursor.getString(cursor.getColumnIndex(COUNTS));
                lastRowTime = cursor.getString(cursor.getColumnIndex(TIME));
                lastRowTableName = cursor.getString(cursor.getColumnIndex(TABLE_NAME));

            } while (cursor.moveToNext());
        }

        Intent viewnote = new Intent(Firstpage.this, CounterActivity.class);
        viewnote.putExtra(ID, lastRowId);
        viewnote.putExtra(ROW_TITLE, lastRowTitle);
        viewnote.putExtra(ROW_COUNTS, lastRowCount);
        viewnote.putExtra(ROW_TIME, lastRowTime);
        viewnote.putExtra(ROW_TABLE, lastRowTableName);
        startActivity(viewnote);


        cursor.close();
    } catch (Exception i) {
        i.printStackTrace();
    }
});

//CounterActivity.class
Bundle extras = getIntent().getExtras();
 long rRowID = Long.valueOf(extras.getString(Firstpage.ID);
 long rTitle = Long.valueOf(extras.getLong(Firstpage.ROW_TITLE));
 long rCount = Long.valueOf(extras.getLong(Firstpage.ROW_COUNTS));
 long rTime = Long.valueOf(extras.getLong(Firstpage.ROW_TIME));
 long rTable = Long.valueOf(extras.getLong(Firstpage.ROW_TABLE));

答案 1 :(得分:0)

我修复了在SharedPreferences上保存RowID号的问题。

当用户进入CounterActivity时,RowID将自己保存到SharedPreferences。

我创建了一个名为CounterActivity2的CounterActivity副本。

在第一页按钮中,我选择了CounterActivity2 Start Commands。

在它的开头,RowID从Sharedpreferences接收....

谢谢大家。