-2

refer below posts but can't help for me

Problem is i'll change data and click on Save Button it's can't update my data

when i print my ID then it's say NULL

Update Query :

db.update(COMPANY, values, KEY_ID + " = ?",
                new String[] { String.valueOf(company.getId()) });

Java code:

else{
    count = 0;

Company company = new Company();                                   
company.setName(etCompanyName.getText().toString().trim())                            
company.setWebsite(etWebsite.getText().toString().trim());
company.setEmail(etEmail.getText().toString().trim());                                
company.setPhoneHome(etPhoneHome.getText().toString().trim());                                    
company.setPhonePrimary(etPhonePrimary.getText().toString().trim());                                
company.setAddressLine1(etAddressLine1.getText().toString().trim());                                    

 long isUpdated = myDb.updateCompany(company);

 //Show id hear But it's null
 Log.e("TAG", "Id : " + company.getId());

 if (isUpdated != -1) {
 Toast.makeText(getApplicationContext(), "Company Update Successfully: " + isUpdated, Toast.LENGTH_SHORT).show();
 } else {
        Toast.makeText(getApplicationContext(), "Something wrong", Toast.LENGTH_SHORT).show();
         }
     Log.e("Button..."," Update Button Clickd ");
     finish();
}

When i click on Save Button it's print Toast msg : Company Update Successfully 0 every time

Ali
  • 3,346
  • 4
  • 21
  • 56
  • I don't see in your code where you set the id. Where is the line `company.setId`? – RubioRic May 07 '18 at 12:38
  • i can't set id i was just Use LOG to see id for conformation data update or not but id is null @RubioRic – Ali May 07 '18 at 12:40
  • I don't understand you, your update query requires an id. If you don't establish one, no row will be updated – RubioRic May 07 '18 at 12:46
  • simply i click on Save button data can't update and show toast msg : `Company Update Successfully 0` every time @RubioRic – Ali May 07 '18 at 12:48
  • Yes, I have already understood your question. No record will be updated if you don´t set an id. – RubioRic May 07 '18 at 12:49
  • how to i set id ?? @RubioRic – Ali May 07 '18 at 12:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170542/discussion-between-mohammad-ali-and-rubioric). – Ali May 07 '18 at 12:51
  • You are editing an existing company. You already have that attribute. How do you recover the company name to show it and allow the edition? Just the same. I'm not familiar with Android but maybe you can use a hidden field (like the ones used in HTML forms) or pass somehow the id between Activities. – RubioRic May 07 '18 at 12:55
  • using putextra pass the data but how to pass the id i don't know @RubioRic – Ali May 07 '18 at 12:57
  • you can not give id there for update to perform. –  May 07 '18 at 13:05
  • where to bug in my code or which code i miss ?? @AndroidTeam – Ali May 07 '18 at 13:07
  • when you passing company object into update method but that object do not set any id there for not update your record. –  May 07 '18 at 13:08
  • 1
    where are you setting the Id : you do like this company.setId(System.currentTimeMillis()) ; In the model class make the Id as a long – Amit Ranjan May 07 '18 at 13:10
  • this is not work for me @AmitRanjan – Ali May 07 '18 at 13:47
  • 1
    Include the id value just the same as you do with the other fields (name, website, ...). Somehow you have recovered these fields values to show them. You have to recover the id before executing the update. Something like this: `company.setId(getIntent().getExtras().getString("id"));` – RubioRic May 07 '18 at 19:51
  • Thank you so much for giving me a idea, i'll change some code and it's work now again **Thank You** @RubioRic – Ali May 08 '18 at 05:01

1 Answers1

0

Code added..

Company company = new Company();   
company.setId(1);                            
company.setName(etCompanyName.getText().toString().trim()) 
...
long isUpdated = myDb.updateCompany(company);
  • no when you set different id and different data it update. –  May 07 '18 at 13:15
  • every time i wan to change like `company.setId(1);` , `company.setId(2);` , like show on........ – Ali May 07 '18 at 13:17
  • no , suppose you display data remove recycler view and user select any item it display details view that time you get id that id set to company and update it. –  May 07 '18 at 13:19
  • ohhk i'll just try this my was run in my let see it's work for me or not – Ali May 07 '18 at 13:20
  • but hear is 1 problem i'ts iffect another record – Ali May 07 '18 at 13:27
  • if your id is unique in table then it not effect to another record. –  May 07 '18 at 13:28
  • id was unique if i update 1st record it's work but if i update 2nd record it's change the all record same as 2nd record – Ali May 07 '18 at 13:29
  • because all other data are same in company object. –  May 07 '18 at 13:32
  • there from both record same data. –  May 07 '18 at 13:33
  • so what can i do for this – Ali May 07 '18 at 13:33
  • That time you need to show details of particular one record into other activity and show all record into editttext box when user press update button then get all data into company object and pass update method. –  May 07 '18 at 13:41
  • how can i perform this ? – Ali May 07 '18 at 13:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170580/discussion-between-mohammad-ali-and-android-team). – Ali May 08 '18 at 04:58
  • can you please help me for this solution ? – Ali May 08 '18 at 04:58