Click Listerner on update Button. I dont know whether i should use
uid or not.I am trying to update firstname, lastname, username,
password and i dont know about uid. How can i use uid to update my
fields
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String firstName = etFirst2.getText().toString();
String lastName = etLast2.getText().toString();
String userName = etUser2.getText().toString();
String password = etPass2.getText().toString();
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "Users").allowMainThreadQueries().build();
UserDao userDao = db.userDao();
User user = userDao.updateUser(firstName,lastName,userName,password);
Toast.makeText(HomeActivity.this, "Updated", Toast.LENGTH_SHORT).show();
}
});
> userDao query statement in userDao file
#Query("update Users set firstName = :firstName, lastName= :lastName, userName= :userName, password= :password")
void updateUser(String firstName,String lastName, String userName, String password);
> This is my class Structure
#Entity(tableName = "Users")
public class User {
#PrimaryKey(autoGenerate = true)
public int uid;
public String firstName;
public String lastName;
public String userName;
public String password;
public User(String firstName, String lastName, String userName, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
}
Related
Im trying to compare the users input (username and password) with the user objects (eg user1, user5) to see if they match and if they do then the user will be directed into another activity because they would've been able to successfully logged in, and if not a message will be displayed.
Ive created a class called User which I call to get the userName and password. I think a if statement will work for this to compare them by using .equal but when I try the app no matter if the input is correct or not it buts the message that the details were incorrect. Its like it is fully ignoring all my if statements apart from the last one. Can anyone tell me if there is a better way to achieve this or if there is something wrong in my code? Errors aren't showing up anywhere.
User class:
public class User {
// Instance variables
static String userName;
static String password;
static String favColor;
// User constructor
public User(String initUserName, String initPassword, String initFavColor) {
userName = initUserName;
password = initPassword;
favColor = initFavColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
Main Activity:
logInBt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
User user1 = new User("Jason", "Sword", "Red");
User user2 = new User("Billy", "Dinosaur", "Blue");
User user3 = new User("Zack", "Elephant", "Black");
User user4 = new User("Trini", "Tiger", "Yellow");
User user5 = new User("Kimberly", "Bird", "Pink");
String userET = userEditText.getText().toString();
String userPassword = passwordEditText.getText().toString();
if(userET.equals(user1.getUserName()) & userPassword.equals(user1.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
} else if(userET.equals(user2.getUserName()) && userPassword.equals(user2.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
}else {
Toast.makeText(getApplicationContext(), "Incorrect details. Please try again", Toast.LENGTH_SHORT).show();
}
}
});
The properties of an object must not be of the static type,
the static value is kept while the application is in use,
therefore, each time an instance of the object is created, it acquires a new value
Solution:
public class User {
//Instance variables
private String userName;
private String password;
private String favColor;
// User constructor
public User(String userName, String password, String favColor) {
this.userName = userName;
this.password = password;
this.favColor = favColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
}
I can recommend you read about static variables
I am trying to experiment and learn BuilderPattern,reading from json file at the same time. My goal is to create object using the data I get from the json file. This is how the json file looks like:
[{
"firstName": "Git",
"lastName": "Hub",
"website": "howtodoinjava.com"
},
{
"firstName": "Brian",
"lastName": "Schultz"
}
]
Class1: Employee Class
public class Employee {
private String firstName; // required
private String lastName; // required
private String website; // optional
//Only has setters
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setWebsite(String website) {
this.website = website;
}
//no public constructor. So the only way to get a Employee object is through the EmployeeBuilder class.
private Employee(EmployeeBuilder builder) {
this.firstName = firstName;
this.lastName = lastName;
this.website = website;
}
public static class EmployeeBuilder {
private String firstName; // required
private String lastName; // required
private String website; // optional
public EmployeeBuilder() throws IOException {
}
public EmployeeBuilder requiredFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public EmployeeBuilder requiredLastName(String lastName) {
this.lastName = lastName;
return this;
}
public EmployeeBuilder optionalWebsite(String website) {
this.website = website;
return this;
}
//Return the finally constructed User object
public Employee build() {
Employee emp = new Employee(this);
return emp;
}
}
}
Class2: User Class
public class User {
String firstName;
String lastName;
String website;
//Using Getters to get values from Json
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getWebsite() {
return website;
}
}
3 Main class
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper object = new ObjectMapper();
User[] user = object.readValue(new File("C:\\MyTemp\\jackson.json"), User[].class);
//Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).requiredLastName(person.getLastName()).optionalWebsite(person.getWebsite()).build();
List<User> emp1 = object.readValue("C:\\MyTemp\\jackson.json", new TypeReference<List<User>>() {});
emp1.stream().forEach(x -> System.out.println(x));
/*for (User person : user) {
Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).build();
System.out.println(employee);
} */
}
}
Problem: When I run this code all I get are the 1st names. Git and Brian. I am not getting the last names or website.
Can someone please suggest what am I missing? Thanks in advance for your time.
i was try to build real time car rent but i got this error returning null of datasnapshot and tried all fix thats impossible without any success
i don't know where is a problem and why my database dosn't response the request
DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference(Common.driver_location_tbl);
GeoFire gf = new GeoFire(driverLocation);
GeoQuery geoQuery = gf.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),distance);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
#Override
public void onKeyEntered(final String key, final GeoLocation location) {
FirebaseDatabase.getInstance().getReference(Common.driver_tbl)
.child(key)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Rider rider = dataSnapshot.getValue(Rider.class);
mMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude,location.longitude))
.flat(true)
.title("Driver Name :"+rider.getUsername())
.snippet("Phone : "+rider.getPhone())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.cars)));
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and my Rider class
public class Rider {
private String email,password,phone,username;
public Rider() {
}
public Rider(String email, String password, String phone, String username) {
this.email = email;
this.password = password;
this.phone = phone;
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
How to fix app crach and returning null of datasnapshot ?
I have research for a fix and have not found anything.
ok i fixed it
problem was i register users with name not Uid so when i request name and phone got n
This question already has an answer here:
How to send form input values and invoke a method in JSF bean
(1 answer)
Closed 5 years ago.
I have a JSF project, and I am trying to do a login page, in my project I have a managed bean that has a validate method for the username and password, and I have a bean class with setters and getters which has the user info that get filled for a database eg.(username, password, isActive, Full name), my question is how can I call the user info in JSF el expression in my xhtml pages if they are not in the managed bean?
Here is my java bean:
#Table(name="students_info")
public class User {
#Column(name="std_record_id")
private int id;
#Column(name="std_id")
private String userId;
#Column(name="first_name")
private String firstName;
#Column(name="web_password")
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
And here is my MBLogin:
#ManagedBean
#SessionScoped
public class MBLogin {
User user = new User();
LoginDAO loginDao = new LoginDAO();
public String validteStudent() {
boolean valid = loginDao.validateStudent(user.getUserId(), user.getUserId());
if (valid) {
HttpSession session = SessionUtils.getSession();
session.setAttribute("username", user);
return "student";
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect Username and Passowrd", "Please enter correct username and Password"));
return "login";
}
}
}
Add a getter for the user:
#ManagedBean
#SessionScoped
public class MBLogin {
User user = new User();
LoginDAO loginDao = new LoginDAO();
public String validteStudent() {
boolean valid = loginDao.validateStudent(user.getUserId(), user.getUserId());
if (valid) {
HttpSession session = SessionUtils.getSession();
session.setAttribute("username", user);
return "student";
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect Username and Passowrd", "Please enter correct username and Password"));
return "login";
}
}
public User getUser() {
return user;
}
}
Then in your xhtml you can call it like this:
#{user.id}, #{user.firstName}
I am getting JSON String data like
{"username":"KU","password":"KU"}.
How to convert this string to JAXBElement object.
Please give me answer.
You can use Jackson to unmarshal JSON easily. See the code below for your username and password case above. It outputs the following to the console console showing it has created an instance of the class from the JSON string.
{"username":"KU","password":"KU"} -> Username [KU], Password [KU].
import org.codehaus.jackson.map.ObjectMapper;
public class JaxbTest {
public static void main(String[] args) throws Throwable {
String json = "{\"username\":\"KU\",\"password\":\"KU\"}";
ObjectMapper mapper = new ObjectMapper();
JavaObject javaObject = mapper.readValue(json, JavaObject.class);
System.out.println(json + " -> " + javaObject.toString());
}
private static class JavaObject {
private String username;
private String password;
public JavaObject() { }
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Override
public String toString() {
return "Username [" + this.username + "], Password [" + this.password + "]";
}
}
}