i am here to ask for help again, my problem is that in first activity i have values that i insert into database , in the second activity when i try to insert the values it creates a new row, what i want is the values of second activity completes the row of the first activity. And also how could i refer to the last row ??
i don't know how to do it! so, is there any ideas ?
public void updateMENAGE(int id, int a16, int b17, String rm_18_1ts,
String rm_18_2ts, int c19, int d20, int e21) {
ContentValues values = new ContentValues();
values.put(col_Type_habitat,a16);
values.put(col_Statut_occupation ,b17);
values.put(col_Nombre_ménages_habitant_logement,rm_18_1ts);values.put(col_Nombre_pièces_occupes_ménage,rm_18_2ts);
values.put(col_Mode_principal_approvisionnement_eau_potable, c19);
values.put(col_Mode_principal_éclairage,d20);
values.put(col_Mode_principal_assainissement,e21);
db.update(MENAGE,values,_id+"="+id, null);
}
public class ActivityDeux extends Activity {
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
db.updateMENAGE(1,a16,b17,rm_18_1ts,rm_18_2ts,c19,d20,e21);
db.close();
}
You don't need to insert new record. You need to update existing record. That is your solution
Related
updated my code. My issue happens when i back out of the activity. Listview items are lost. I checked the Sqlite database and all items are saved, just not showing up again on listView when I reStart-Activity.
MainActivity
private ListView lst;
private CustomeAdapter cv;
private EditText nameEd, middleEd, lastEd;
private ArrayList<People> peopleArrayList;
private DataHelper myData;
peopleArrayList = new ArrayList<>();
OnCreate.....
public void addPerosn(View view) {
String myName = nameed.getText().toString();
String myMiddle = middleed.getText().toString();
String myLast = lasted.getText().toString();
boolean insert = myData.addData(myName, myMiddle, myLast);
if (insert == true) {
peopleArrayList.add(new People(myName, myMiddle, myLast));
cv = new CustomeAdapter(this, peopleArrayList);
lst.setAdapter(cv);
nameed.setText("");
middleed.setText("");
lasted.setText("");
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
My DataHelper method i want to call to Show All
public Cursor showData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
Any suggestions are appreciated . Thanks
Make sure you have overridden getCount and it returns proper count.
#Override
public int getCount() {
return items.length;
}
Apart from above solution, I would recomment you to do it in proper way
a) Create a model/pojo class say Person which will have firstName,lastName and middleName
b) create a data set of Person, i.e list of person
c) create a method addPerson in adapter class, and call whenever you want to add new Person data into the list. addPerson method will also refresh the adapter by calling notifyDataSetChanged
d) In activity create adapter object only once, later on just use method of it say adapter.addPerson(person)
While inserting my listview gets refreshed automatically but not update when the item in the listview is updated. It only updates on database. I can see the listview is updated when I close the application and open again, or come back from previous activity.
I found some discussion related to my problem. Like: Refresh ListView with ArrayAdapter after editing an Item . Her I found that make a new method to populate the Listview and call it in the onResume method of your activity.
And the problem has been solved using this. But I do not get how to make new method mentioned like there. Could anybody help me to make it understandable?
My code in activity class:
personNamesListView = (ListView) findViewById(R.id.traineeslist);
traineeListAdapter = new ArrayAdapter<Trainee>(this,
android.R.layout.simple_list_item_1,
currentTraining.getTraineeArrayList());
personNamesListView.setAdapter(traineeListAdapter);
protected void onResume() {
super.onResume();
}
And this way I populated my personNamesListView using method stringToString() in model class;
public void loadTraineeList() {
DatabaseHelper db = DatabaseHelper.getInstance();
this.traineeArrayList = new ArrayList <Trainee>();
Cursor cursor = db.select("SELECT * FROM person p JOIN attendance a ON p._id = a.person_id WHERE training_id="+Integer.toString(this.getId())+";");
while (cursor.moveToNext()) {
Trainee trainee = new Trainee();
trainee.setID(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PERSON_ID)));
trainee.setFirstname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_FIRSTNAME)));
trainee.setLastname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_LASTNAME)));
trainee.setJobTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_JOBTITLE)));
trainee.setEmail(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_EMAIL)));
trainee.setCompany(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_COMPANY)));
trainee.setDepartment(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_DEPARTMENT)));
trainee.setBadgeNumber(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_BADGE)));
// Pass to the arraylist
this.traineeArrayList.add(trainee);
}
}
public ArrayList<Trainee> getTraineeArrayList() {
return traineeArrayList;
}
public void setTraineeArrayList(ArrayList<Trainee> traineeArrayList) {
this.traineeArrayList = traineeArrayList;
}
I insert and Update data into database into one method:
public void storeToDB() {
DatabaseHelper db = DatabaseHelper.getInstance();
db.getWritableDatabase();
if (this.id == -1) {
// Person not yet stored into Db => SQL INSERT
// ContentValues class is used to store a set of values that the
// ContentResolver can process.
ContentValues contentValues = new ContentValues();
// Get values from the Person class and passing them to the
// ContentValues class
contentValues.put(DatabaseHelper.PERSON_FIRSTNAME, this
.getFirstname().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_LASTNAME, this
.getLastname().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_JOBTITLE, this
.getJobTitle().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
contentValues.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
.trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_DEPARTMENT, this
.getDepartment().trim().toUpperCase());
contentValues.put(DatabaseHelper.PERSON_BADGE, this
.getBadgeNumber().trim().toUpperCase());
// here we insert the data we have put in values
this.setID((int) db.insert(DatabaseHelper.TABLE_PERSON,
contentValues));
} else {
// Person already existing into Db => SQL UPDATE
ContentValues updateTrainee = new ContentValues();
updateTrainee.put(DatabaseHelper.PERSON_FIRSTNAME, this
.getFirstname().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_LASTNAME, this
.getLastname().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_JOBTITLE, this
.getJobTitle().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
updateTrainee.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
.trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_DEPARTMENT, this
.getDepartment().trim().toUpperCase());
updateTrainee.put(DatabaseHelper.PERSON_BADGE, this
.getBadgeNumber().trim().toUpperCase());
db.update(DatabaseHelper.TABLE_PERSON, updateTrainee,
DatabaseHelper.PERSON_ID+"= ?", new String[]{Integer.toString(this.getId())});
System.out.println("Data updated");
}
}
You should call traineeListAdapter.notifyDataSetChanged() whenever you update your ArrayList representing the items in the ListView.
There's a similar question here that can give you some help.
Although I've accomplished something similar using
yourlistview.invalidateViews()
after changing the data to show in the listview
when notifyDataSetChanged() didn't work.
EDIT:
After making all the operations in the data that I want to show i just set the adapter and try to refresh my listview by calling invalidateViews().
selectedStrings = new ArrayList<String>(typeFilterStrings);
adapter.setArrayResultados(selectedStrings);
listTypeFilter.invalidateViews();
It's not obligatory to set the adapter again in my case worked.
use like this:
Create an instance of your custom adapter, so you can use it anywhere you like...
public class ScoreList extends SherlockFragmentActivity {
private ListView listViewScore;
private ScoreListAdapter adapter;
static List<Score> listScore = new ArrayList<Score>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_list);
ctx = this;
listScore = dbh.getAllScores();
listViewScore = (ListView) findViewById(R.id.score_list);
adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
listViewScore.setAdapter(adapter);
((BaseAdapter) listViewScore.getAdapter()).notifyDataSetChanged();
}
}
By the way, if your listScore array is already loaded, then you do not need to use
adapter.notifyDatasetChanged();
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
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.
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
I feel like i am missing something simple and stupid. I have a list view with a few buttons at the top. The list view is initially populated with data. When you click a button the list view is supposed to populate its self based on a changed variable in the Where statement. In reality i could probably just start a new List activity but i feel like there is a better way.
I have been reading up on CursorAdapter.changeAdapter() and notifydatasetchanged() I have not implemented this yet because i am having a more basic problem.
I can successfully query the database and display the static results in the list. When i try to break process into steps i am running into an ERROR: Invalid statement in fillWindow. The best i understand this is caused by improperly closing cursors databases and DB helpers and for this reason people use content providers.
For now i am just trying to get this to work.
public class DListView extends ListActivity implements OnClickListener{
public static final String NAME = "Name";
public static final String DESCRIPT = "Description";
public static final String DATABASE_TABLE = "Table";
public static final String DAY = "Day_id";
/** Called when the activity is first created. */
private Cursor c = null;
private String[] colsfrom = {"_id", NAME, DESCRIPT, DAY};
private int[] to = new int[] {R.id.text01, R.id.text02, R.id.text03, R.id.text04};
public int b = 0;
public int d = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinklistview);
View left = findViewById(R.id.left_button);
left.setOnClickListener(this);
View right = findViewById(R.id.right_button);
right.setOnClickListener(this);
Intent thisIntent = getIntent();
b = thisIntent.getIntExtra("_b", 0);
//0 is the default argument is nothing is passed.
d = thisIntent.getIntExtra("_d", 0); //same idea as above.
c = fillList();
/*this creates a new cursor adapter
#param Context is the list context that you will be filling.
#param int layout is the layout that you will use for the rows
#param Cursor is the cursor that was returned from the query
#param from is the column names
#param to is the layout ids that the fields will be put in.
#param from is the column names to map from
#param to is the layout ids that the column fields will be put in.
*/
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.row, c, colsfrom, to);
setListAdapter(myAdapter);
}
private Cursor fillList() {
DBHelper DbHelper = new DBHelper(this);
Cursor cursor;
String wHERE = "_id = " + b + " AND Day_id = " + d ;
try {
myDbHelper.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
cursor = myDbHelper.getDrinks(DATABASE_TABLE, colsfrom, wHERE, null, null,null, null);
myDbHelper.close();
return cursor;
}
When i put the contents of fillList() in the onCreate() it displays data just fine. When i pull it out it gives me the ERROR. Why is this happening? If anyone has a better way of going about this i would love to read it. Or we can play a game called "What stupid thing am i doing wrong Now?
Thankyou.
EDIT:From DBHelper
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
#Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
I am thinking that my problem line is the super.close() I believe that this line closes the database and anything affiliated with it which means the cursor that i try to use after its closed. I may be wrong though. Please explain if you can.
Your problem is right here, in your fillList():
myDbHelper.close(); // <--- here
return cursor;
you make a cursor object but close your database connection before you even get to use it (this component of the database) which would render it useless or null if you would. Usually you close the cursor and then the database. But that's not throwing the error. That error specifically is because you hooked up this cursor to a cursorAdapter trying to fill your listView with nothing. Move that and it should be gone.
So where do you move it then? If you have a cursor hooked up to listView, it needs to be open the entire time, otherwise you'll get another error saying "attempting to re-open an already closed object". I'd suggest putting in the onDestroy() when then listView is being chucked as well.
YaY Solved. Mango is exactly correct. Thankyou for you suggestion to close cursor in on destroy. I am not sure if the super.close() line closes my cursor or not. but i will look into it. I am also going to put the database query in async task for kicks and giggles.
I simply moved the two lines that created a new SimpleCursorAdapter and set the list view into the fillList method.
I also implemented my buttons and just added fillList at the end.
Here is the code that fixed things. Simple Mistake.
private void fillList() {
DBHelper DbHelper = new DBHelper(this);
Cursor cursor;
String wHERE = "_id = " + b + " AND Day_id = " + d ;
try {
myDbHelper.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
cursor = myDbHelper.getDrinks(DATABASE_TABLE, colsfrom, wHERE, null, null,null, null);
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, colsfrom, to);
setListAdapter(myAdapter);
myDbHelper.close();
}
And Here is wehre i call the fillList again that updates my list view.
public void onClick(View v) {
switch(v.getId()) {
//Mess with d based on button click
}
fillList();
}
Now the application has to create a new simple cursor adapter every time something is changed.
If anyone has any ideas on implementing this without creating a new CursorAdapter every time that would help very much but my initial problem is solved. Thankyou for your help. Just the fact that you wanted to see my stack trace told me that i was not doing anything wrong in the code that i initially presented and i forgot that i made my dbHelper close all connections. Thankyou mango. I solved this last night but couldnt post it. Thanks for the explanation good sir. If you have any insight to the constant creation of a new cursoradapter i would be very pleased to see it. Maybe i need to fix the super.close() command somehow.
When I insert values in sqlite android, does it happens once or the database is created every time I run my application?
Specifically I have categories that will populate a list (food, drinks ....etc).
Where do I put my insert method?
In my dbHelper class or the class that needs the data?
public class ShoppingCategories extends ListActivity{
private AppSQLite mDbHelper;
private Cursor mNotesCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new AppSQLite(this);
insertShoppingCategories();
}
public void insertShoppingCategories(){
mDbHelper.open();
long id;
id = mDbHelper.createShoppingCategory("food", 5, "#drawable/ic_launcher");
id = mDbHelper.createShoppingCategory("drink", 3, "#drawable/ic_launcher");
mDbHelper.close();
}
and the createShoppingCategory from dbHelper class is:
public long createShoppingCategory(String name, int priority, String icon) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_PRIORITY, priority);
initialValues.put(KEY_ICON, icon);
return mDb.insert(tShopCateg, null, initialValues);
}
Database is created only if it doesn't exist. If there are values need to be inserted only once you should put them inside onCreate inside your dbHelper class.
The Database is created once when you create the first instance of the mDbHelper.open(); (this may change depending on your implementation of the DB Helper class onCreate() method). The data you insert in the DB remains in the DB even after the App is closed (this is the reason for having a DB). You use the insert function when you need to save user input in the database persistantly.
put it where you really want to have functionality to insert data at a particular event/instance. You define how you DB would be (table rows and column) in dbHelpr and performs operation like insert delete in the activity.And yes, DB is created only the first time run of your app when you create the object of dbHelper and db._exec command is executed.
Once your database is created it will recreate only if your version of DB is changed or when you clear your data from settings>>Application>>manage Application>>clear data.
And if you want to insert your data once then you should put that code in onCreat() of dbHelper class.