-4

i made a class User with properties in user.java file and want to access that class in other activity. i made the object of that class in main activity but its not working

public class User{

    public static int id;
    public static String name;
    public static String desg;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        User.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        User.name = name;
    }
    public String getDesg() {
        return desg;
    }
    public void setDesg(String desg) {
        User.desg = desg;
    }

}

public class MainActivity extends Activity {

        User user = new User();
        EditText name = (EditText)findViewById(R.id.name);
        EditText desg= (EditText)findViewById(R.id.desg);
        EditText id = (EditText)findViewById(R.id.id);

    ArrayList<user> userlist = new ArrayList<user>();
    Button btn;
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
hira
  • 29
  • 5

3 Answers3

4

Write Capital "U" instead of small "u" like below

ArrayList<User> userlist = new ArrayList<User>();

and you will be able to access User class in Arraylist

Akshay Panchal
  • 695
  • 5
  • 15
0

I guess you wanna get edittext's content to the User class object and add this object to arraylist. If so, you can do this part like that.

public class MainActivity extends Activity {
    ...

    User user = new User();
    EditText name = (EditText)findViewById(R.id.name);
    EditText desg= (EditText)findViewById(R.id.desg);
    EditText id = (EditText)findViewById(R.id.id);

    user.setName(name.getText().toString());
    user.setDesg(desg.getText().toString());
    user.setId(Integer.parseInt(id.getText().toString()));

    ArrayList<User> userlist = new ArrayList<User>();
    userlist.add(user);
}

And if you send this userlist to another activity, you have to use parcelable. So you can check Raghunandan's answer

Community
  • 1
  • 1
-1

Write ('u' is capital here)

ArrayList<User>userlist = new ArrayList<User>();

instead of

ArrayList<user>userlist = new ArrayList<user>();