user9228302
user9228302

Reputation:

Room Database: When i update the list it works completely fine! but when i reopen the app it shows old data

Dao

@androidx.room.Dao

public interface Dao {

@Insert
public void addUser(User user);

@Query("SELECT * FROM users")
public List<User> getUsers();

@Update
public void updateUser(User user);

@Delete
public void deleteUser(User user);

}

Adapter class

holder.updateUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(mctx);

            LayoutInflater inflater = LayoutInflater.from(mctx);
            View view1 = inflater.inflate(R.layout.dialog_update_user, null);
            builder.setView(view1);

            final EditText etFNAme = view1.findViewById(R.id.editTextFirstNameUser);
            final EditText etLName = view1.findViewById(R.id.editTextLastNameUser);

            etFNAme.setText(user.getFirstName());
            etLName.setText(user.getLastName());

            final AlertDialog dialog = builder.create();
            dialog.show();

            view1.findViewById(R.id.updateUserBtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String fname = etFNAme.getText().toString().trim();
                    String lname = etLName.getText().toString().trim();

                    MainActivity.database.dao().updateUser(user);

                    holder.textViewFirst.setText(fname);
                    holder.textViewLast.setText(lname);

                    Toast.makeText(mctx, "User Updated", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            });
        }
    });

i tried to make a new list and tried to call Dao().getUser() it didn't work again when i update the specific data it works! but when reopen the app it shows old data not the udpated data?

Mainactivity

public class MainActivity extends AppCompatActivity {

EditText etFirstname, etLastname;
Button btnSave;

public static AppDatabase database;

RecyclerView recyclerView;
UserAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "userdb").allowMainThreadQueries().build();

    etFirstname = findViewById(R.id.editTextFirstName);
    etLastname = findViewById(R.id.editTextLastName);

    btnSave = findViewById(R.id.saveBtn);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String fName = etFirstname.getText().toString();
            String lName = etLastname.getText().toString();

            if (fName.isEmpty() || lName.isEmpty()) {
                Toast.makeText(MainActivity.this, "please enter data!", Toast.LENGTH_SHORT).show();
            } else {
                User user = new User();
                user.setFirstName(fName);
                user.setLastName(lName);
                database.dao().addUser(user);
                Toast.makeText(MainActivity.this, "User Added!", Toast.LENGTH_SHORT).show();

                adapter.add(user);
            }
        }
    });
    adapter = new UserAdapter(this, database.dao().getUsers());
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    recyclerView.setAdapter(adapter);
}

}

this is how i set the adapter to mainactivity to add user but i want update the user by clicking the update button which resides in dialog box that i have set in adapter class

Upvotes: 0

Views: 479

Answers (1)

Tal Mantelmakher
Tal Mantelmakher

Reputation: 1002

Looks like you don't actually update the user here:

                String fname = etFNAme.getText().toString().trim();
                String lname = etLName.getText().toString().trim();

                MainActivity.database.dao().updateUser(user);

                holder.textViewFirst.setText(fname);
                holder.textViewLast.setText(lname);

You are missing:

            user.setFirstName(fName);
            user.setLastName(lName);

before MainActivity.database.dao().updateUser(user)

It appears as though the user was updated, when in reality you just updated the TextViews with the new first and last names.

Upvotes: 1

Related Questions