Display contents of an array in a listview - java

Im making a project where the list items are contents with multiple attributes (username and passoword). I want to make the listview only display the usernames and once the username is clicked it would show the users password in a toast message. Im new to programming so i would need a few hints.
I managed to make the class and add 2 users to the array list.
Any ideas on how i could make the listview only display the usernames and then the password once the username is clicked?
Thanks in advance :)
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static class user {
public String username;
public String password;
public user(String name, String pass) {
username = name;
password = pass;
List<user> userList = new ArrayList<>();
userList.add(new user("Tom", "Hello123"));
userList.add(new user("Jack", "Hell1"));
}
}
}

So, it might look like just adding a if statement and depending on what you are useing as a check box, then you could edit accordingly.
if(username == "Tom" ){
Toast.makeText(this, "Your password is: Hello123", LENGTH_LONG);
} else if (username == "Jack"){
Toast.makeText(this, "Your password is: Hell1", LENGTH_LONG);
} else {
Toast.makeText(this, "Invalid or error found", LENGTH_LONG);
}

Related

How can I get hashmap's key inside of the hashmap [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed last year.
I'm trying to make a system without MySQL database which will have accounts and accounts' data. But it didn't work
My accounts java file:
/*
* Instead of using 2 different ArrayList (for Username and Password) we used Hashmap
* HashMap stores keys and the keys' values
* In our HashMap keys refer to usernames values refer to passwords
*/
import java.util.HashMap;
import javax.swing.JOptionPane;
public class Accounts {
//I used static to reach list from another class suggest me another way please
static HashMap AccountList = new HashMap<String, HashMap<String, String>>();
public static void CreateAccount(String userID, String password, String email, String fullName, String department){
//We have to check that there shouldn't be an account with the same username
if (AccountList.containsKey(userID)){
JOptionPane.showMessageDialog(null, "There is an account with that username");
}
else{
HashMap Data = new HashMap<String, String>();
Data.put("userID", userID);
Data.put("password", password);
Data.put("email", email);
Data.put("fullName", fullName);
Data.put("department", department);
AccountList.put(userID, Data);
JOptionPane.showMessageDialog(null, "Member created successfully");
}
}
}
In my login.java I get an error on that line
boolean IsLoginSuccessful = false;
//We are getting username and password text/string from the form
String userID = username_field.getText();
String password = password_field.getText();
//Now check if username is in list then check if username's password equals to password
if (Accounts.AccountList.containsKey(userID)){
String userPassword = Accounts.AccountList.get(userID).get("password");
IsLoginSuccessful = true;
}
That second ".get" gives error like "cannot find symbol". Why does it take the hashmap as an object element? Also if you have another way to store these datas without hashtable and check the password etc. Show me please thank you.
To get the keys you would do
Set<String> keys = AccountList.keySet();
And you should declare your map as
Map<String, Map<String,String>> accountList = new HashMap<>();
Map<String,String> data = new HashMap<>();
But it would be best if you would declare a user class and put that in the map.
class User {
private String username;
private String password;
...
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
Map<String, User> accountList = new HashMap<>();
You could then use defined getters (e.g. user.getPassword(),
user.getUsername()) to retrieve the information.
Then it might be used as follows:
String username = get_from_console.
User user = accountList.get(username);
String password = user.getPassword();

Firebase realtime database doesn't update database when i sign up

I am trying to send information to the firebase database, I am connected and already checked multiple times and i can't find the error here's my code
Here's the database reference command
everything is well put together and working, i don't exactly know why it doesn't really allow me to send information doesn't show any errors and everything
userInformation = FirebaseDatabase.getInstance().getReference("Users").child("Users");
firebaseAuth = FirebaseAuth.getInstance();
private void saveUserInformation(){
String UserName = NameET.getText().toString();
String Userbirthday = dateBirth.toString();
String Userdate_drivinglicense = dateLicense.toString();
String UserCountry = Countries.getSelectedItem().toString();
String UserGender = Genders.getSelectedItem().toString();
String HouseLocationLat = String.valueOf(location.getLatitude());
String HouseLocationLong = String.valueOf(location.getLongitude());
Userinformation userInfo = new Userinformation(UserName,Userbirthday,Userdate_drivinglicense,UserCountry,UserGender,HouseLocationLat,HouseLocationLong);
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
userInformation.child(user.getUid()).setValue(userInfo);
}
Toast.makeText(UserInformationActivity.this, "Saving Information", Toast.LENGTH_SHORT).show();
}
The Issue i had was that i didn't implent an Getter and Setter in my Java Class which is connected to the UserInformation, and i had to create a new child in the database with the name "Users" since it wasn't created automatically as it should be, also changed the names of variables in my
Userinformation userInfo = new Userinformation(UserName,Userbirthday,Userdate_drivinglicense,UserCountry,UserGender,HouseLocationLat,HouseLocationLong);
So it can connect to the database correctly Thanks for all the help :)

how to pass parameters (username and password) to function signinactivity

Here I got another class called signinactivity which I wanted to pass username and password to that particular function.
But it seems like something wrong with my code. Anyone got idea with the code?
public void loginPost(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
new SigninActivity().execute(username, password);
}
execute the method with param.
new SigninActivity(username, password).execute();
You can receive param on the method using a constructor.
public SigninActivity(String username,String password) {
this.username = username;
this.password = password;
}

Java, Generating a dynamic object name [duplicate]

This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
I was trying to generate a dynamic object names that will change everytime an object is created. Something like:
first object name would be like "userName" and the following would be like "userName1".
I'm new to java, and I tried to initialize int count=0 and count++ to do it, but the User class doesn't seem to allow me to do that like userName+count. Therefore, is there anyway i could possible go about doing it?
I have search through all the similar thread about dynamic object name, but it doesn't seems to work out for my case.
EDIT
So i have this app built up, that allow user to create account.
And, now i encounter this problem where whenever i tried to create new account,
Firebase clear away all my previous data that i have inserted, and replace it with the current inserted data.
Here's my code:
Main.java
name = (EditText) findViewById(R.id.edit_rName);
email = (EditText) findViewById(R.id.edit_rEmail);
password = (EditText) findViewById(R.id.edit_rPassword);
Button btnSubmit = (Button) findViewById(R.id.btn_rSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Firebase f = new Firebase("https://xxx.firebaseio.com/");
Firebase usersRef = f.child("users");
Map<String, User> users = new HashMap<String, User>();
users.put(name.getText().toString(), new User(name.getText().toString(), password
.getText().toString(), email.getText().toString()));
usersRef.setValue(users);
}
});
User.java
public class User {
private String fullName;
private String password;
private String email;
public User(String fullName, String password,String email) {
this.fullName = fullName;
this.password = password;
this.email = email;
}
public String getPassword() {
return password;
}
public String getFullName() {
return fullName;
}
}
You should use an array if you know in advance how many objects you will create, and a list if you don't know in advance.
Array:
String[] usernames = new String[4];
usernames[0] = "Alex";
usernames[1] = "Bob";
usernames[2] = "Carol";
usernames[3] = "David";
//usernames[4] = "Eliza"; // won't work, out of bounds!
System.out.println(usernames[2]); //prints Carol
List(recommended) (first import java.util.ArrayList):
List<String> usernames = new ArrayList<String>();
usernames.add("Alex");
usernames.add("Bob");
usernames.add("Carol");
usernames.add("David");
usernames.add("Eliza");
System.out.println(usernames.get(2)); // prints Carol
usernames.set(2, "Carlos");
System.out.println(usernames.get(2)); // prints Carlos

Struts2 if tag not working

I am writing a login page that when a invalid user tries to login I redirect to the login action with a error parameter equal to 1.
private String username;
private String password;
private int error;
#Override
public String execute()
{
//validate user input
if (username == null || password == null || username.isEmpty() || password.isEmpty())
{
error = 2;
return LOGIN;
}
LoginModel loginModel = new LoginModel(username, password);
HttpBuilder<LoginModel, User> builder = new HttpBuilder<LoginModel, User>(User.class);
builder.setPath("service/user/authenticate");
builder.setModel(loginModel);
IHttpRequest<LoginModel, User> request = builder.buildHttpPost();
User user = request.execute(URL.BASE_LOCAL);
//redirects to login page
if (user == null)
{
error = 1;
return LOGIN;
}
else
{
return SUCCESS;
}
}
//Getters/Setters
If a invalid user trys to login it redirects to localhost:8080/app/login.action?error=1. I am trying to display a error message to user by access the error parameter by using the if tag but its not working the message is not displaying.
<s:if test="error == 1">
<center>
<h4 style="color:red">Username or Password is invalid!!</h4>
</center>
What am I doing wrong?
As far as I'm concerned what you're doing wrong is completely ignoring the framework.
Roughly, IMO this should look more like this:
public class LoginAction extends ActionSupport {
private String username;
private String password;
#Override
public String validate() {
if (isBlank(username) || isBlank(password)) {
addActionError("Username or Password is invalid");
}
User user = loginUser(username, password);
if (user == null) {
addActionError("Invalid login");
}
}
public User loginUser(String username, String password) {
LoginModel loginModel = new LoginModel(username, password);
HttpBuilder<LoginModel, User> builder = new HttpBuilder<LoginModel, User>(User.class);
builder.setPath("service/user/authenticate");
builder.setModel(loginModel);
IHttpRequest<LoginModel, User> request = builder.buildHttpPost();
return request.execute(URL.BASE_LOCAL);
}
}
You would have an "input" result containing the form, and display any action errors present using whatever style you wanted. If it's critical to change the styling based on which type of login error it is you'd have to play a few more games, but that seems excessive.
Unrelated, but I'd move that loginUser code completely out of the action and into a utility/service class, but at least with it wrapped up in a separate method you can mock it more easily. It certainly does not belong in the execute method.
You need to provide the getter and setter of field 'error' to access it from value stack.
public int getError()
{
return error;
}
public void setError(int error)
{
this.error = error;
}
And try to access it in OGNL:
<s:if test="%{#error==1}"></s:if>
Or using JSTL:
<c:if test="${error==1}"></c:if>

Categories