Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well you can just use the primary key. When you insert something into the database you get as a return value the primary key. You can add this to the Intent that opens the other Activity and that way refer back to the row you previously inserted.</p> <p><strong>Edit:</strong></p> <p>I don't know if you are working with and SQLiteDatabase Object or with a ContentProvider, but in any case the code would be pretty much the same. In this example I will work directly with an SQLiteDatabase Object, even though using ContentProviders is in most cases the better alternative.</p> <p>In your first Activity:</p> <pre><code>// When you perform an insert you get the id of the row which was just inserted. long id = sqliteDatabase.insert("some_table", null, contentValues); // The id will be -1 if an error occured if(id &gt;= 0) { ... } ... // When you start your second Activity you can add the id to the Intent Intent intent = new Intent(getApplicationContext(), SecondActivity.class); // The String is a tag with which you can later retrieve the id from the Intent. // Note that you should never hardcode String like that, use constants in your app. intent.putExtra("rowId", id); </code></pre> <p>In the onCreate Method of your second Activity you can retrieve the id:</p> <pre><code>@Override protected void onCreate (Bundle savedInstanceState) { // Check if the Activity has been created for the first time if(savedInstanceState == null) { // Retrieve the Intent with which the Activity was started Intent intent = getIntent(); long id = intent.getLongExtra ("rowId", -1); // If id is valid proceed if(id &gt;= 0) { Cursor cursor = sqliteDatabase.query("some_table", columns, "_id = ?", new String[] { String.valueOf(id) }, null, null, null, null); // Check if Cursor is valid and if yes read your data. if(cursor != null) { ... } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload