Reputation: 131
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 {
final String PREFS_NAME = "MyPrefsFile";
final String ID = "id";
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
Toast.makeText(getApplicationContext(), "Données enregistrées", Toast.LENGTH_LONG).show();
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
long id = db.insertMENAGE(rm_1ts,rm_2ts,rm_3ts);
prefs.edit().putLong(ID, id).commit();
db.close();
}
the second activity code is :
public class ActivityDeux extends Activity {
SharedPreferences prefs2 = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
private SharedPreferences prefs;
long id = prefs.getLong(ID, 0);
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
Upvotes: 0
Views: 382
Reputation: 236
EDIT:
public class ActivityUn extends Activity {
final String PREFS_NAME = "MyPrefsFile";
final String ID = "id";
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
Toast.makeText(getApplicationContext(), "Données enregistrées", Toast.LENGTH_LONG).show();
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
long id = db.insertMENAGE(rm_1ts,rm_2ts,rm_3ts);
prefs.edit().putLong(ID, id).commit();
db.close();
}
}
public class ActivityDeux extends Activity {
final String PREFS_NAME = "MyPrefsFile";
final String ID = "id";
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
SharedPreferences prefs2 = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
long id = prefs.getLong(ID, 0);
//update the value in the table with the id you get from Sharedpreferences
db.insertMENAGE2(id,a16,b17,rm_18_1ts,rm_18_2ts,c19,d20,e21);
db.close();
}
}
You where usin 2 SharedPreferences variables in the second Activity. The second one (the one you were using) was not instantiated. This would throw a NullPointer Exception. Please read the documentation about SQLite and SharedPreferences on developer.google.com and try to understand what the code does. Also try to avoid french words in your code, whenever someone from a non french speaking country has to read/edit your code it may not make sence to them.
Upvotes: 2
Reputation: 2486
Its just like doing an synchronization job you looking for.
I suggest you to use a new table with only one value as 'id'
use a method to update that value. say updatelastupdated(idofinsertedrow)
call this method while inserting the data in the table
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);
updatelastupdated(id) //ADD this method here..
return db.insert(MENAGE,null, initialValues);
}
now the new table idoftheinsertedrow
cell will always has the id(Primary key)
you looking for use that for completing the updation in the table.
To explain you clearly
yourtable say x
activity 1 inserting a row of id 1
activity 2 inserting a row of id 2
activity 3 inserting a row of id 3
now you have another method update()
which must update the row of id 3
for this i suggest keep another table say y
when ever you insert into the table x
you update the value of idupdated
in table y
so always the idupdated
column will hold the last row updated in table x
Upvotes: 1