1

I am here again to ask for help, now i will give details on my problem hoping someone can solve it : in the first activity onClick the method insert is executed

 public class ActivityUn extends Activity {

 DBAdapter db = new DBAdapter(this); 
  public void  ajouter(View v) {

    db.open();
      Toast.makeText(getApplicationContext(), "Données enregistrées", Toast.LENGTH_LONG).show();



        db.insertMENAGE(rm_1ts,rm_2ts,rm_3ts);


           db.close();
      }

the second activity code is :

   public class ActivityDeux  extends Activity {


    DBAdapter db = new DBAdapter(this);
     public void  ajouter(View v) {
        db.open();


        db.insertMENAGE2(id,a16,b17,rm_18_1ts,rm_18_2ts,c19,d20,e21);
        db.close();                     
        } 

and here the two methods insert and update ...

 public long insertMENAGE(String Region, String Provence_prefecture ,StringCommune_Arrondissement) {
  ContentValues initialValues = new ContentValues();
      initialValues.put(col_Region,Region);
      initialValues.put(col_Provence_prefecture ,Provence_prefecture);
      initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement);
       return db.insert(MENAGE,null, initialValues);
  }

In the second activity I will update the same table by completing remaining columns in the row :

  public void insertMENAGE2(int id, int a16, int b17) {
   ContentValues values = new ContentValues();
      values.put(col_Type_habitat,a16);
      values.put(col_Statut_occupation ,b17);
      db.update(MENAGE,values,_id+"="+id, null);
}

Now , I want to indicates the id ( primary key ) of the row in table which is Inserted Last ,

I already looked for solutions but they are not adapted to my situation, since i have other activities updating the same table

And i need to indicates each time that the id concerned is the last one inserted.

Thanks

Utman Alami
  • 131
  • 1
  • 2
  • 12
  • for one, `long id = db.insertMENAGE(...)` gives you the id. – njzk2 Nov 06 '13 at 21:02
  • thanks friend, but how could use this id in other activities ?? – Utman Alami Nov 06 '13 at 21:07
  • could you edit a solution , it would be helpful . – Utman Alami Nov 06 '13 at 21:08
  • see http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – njzk2 Nov 06 '13 at 21:11
  • thanks it is very interresting, but there is no way to add just one query to other acitivities that when database is open, the query has a result the last id ?? that is what i am looking for . thanks again – Utman Alami Nov 06 '13 at 21:16
  • @UtmanAlami How about having a dedicated class with static methods to handle database interaction and use these methods from your activities. You could save the id in your dedicated database interaction class and reuse it as needed. – Lasse Samson Nov 06 '13 at 21:37

1 Answers1

1

To save primitive data for use over multiple activities, your best bet is SharedPreferences.

wvdz
  • 16,251
  • 4
  • 53
  • 90
  • thanks a lot for responding, but could you apply that in my case, i try this solution but i have error!!! – Utman Alami Nov 06 '13 at 22:21
  • The way I use SharedPreferences is by declaring a static variable `SharedPreferences preferences`in my BaseActivity (the abstract Activity for my other Activities to inherit from), and do this in the onCreate: `if (preferences == null) preferences = this.getPreferences(MODE_PRIVATE);` – wvdz Nov 06 '13 at 22:27
  • could you please edit your solution, and apply that to my code – Utman Alami Nov 06 '13 at 22:56