guys I'm very new to the Java word, but i share part of the knowledge because of my c# background, anyways i started developing for android and I'm running into a few snags like the following.
I usually program very OOP so i made all my objects and now i got a very common public class User with things like:
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
and many other properties in it. Also i have a class UserHelper
where i have a populated array with all the users been pulled by the queries in the Helper
public ArrayList< User > getCurrentUsers() { return currentUsers;
}
well... the thing is, i want to be able to populate a spinner with a the value returned from getFirstName as the Display and getId obviously as the Id. I know exactly how to do this in C# but i been trying to fight with Cursors and doing some reading around but nothing, so i figured that it would be an interesting question.
ANYONE CAN SHOW ME HOW TO DO IT PLEASE?
Hi Alex look this examples
http://developer.android.com/resources/tutorials/views/hello-spinner.html
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views
Related
I'm writing an application for Android using DBFlow as ORM library. So, here is one of my tables model:
#Table(database = Database.class)
public class Language extends BaseModel {
#PrimaryKey(autoincrement = true)
long id;
#Column
String key;
#Column
String title;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/* .. Other setters and getters .. */
}
Everything works pretty good, but when I take a look at my DB inspector (I'm using Stetho), I can see 2 identical "id" column:
Its a little bit embarrassing and redundantly.. Isn't it? Is it OK, and what is the cause of this behavior? And if it is not OK, how can I do it right?
So, looks like its Stetho-side feature/bug (according this issue). Just ignore it in production.
I have a model class to store Firebase User information. Inside of the model class I have a HashMap to store all of the data inside. Once I have stored the data, the I push the Hashmap into the Firebase database. The values store fine, but I cannot access the values. Every time I try to access them, I get an error saying that I am attempting to invoke a virtual method on a null object reference.
mDatabase.child("users").child(mUserId).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot ChildSnapshot, String s) {
// These two lines of code give the error.
User author = ChildSnapshot.child("profile").getValue(User.class);
String author_username = author.getUsername();
These give me the error. I am attempting to grab data from the child of the snapshot. Why is this giving me an error? Is there a better way to do this?
JSON Firebase snapshot:
Model class:
//TODO model class for the user. This way I can set the values for each user. I will be adding more values in the future.
public class User {
public HashMap<String, String> hashMap = new HashMap<String,String>();
public User() {
}
public User(String username) {
hashMap.put("username",username);
}
public String getUsername(){
return hashMap.get("username");
}
}
In case somebody else was struggling with this issue, I wanted to give an answer. Inside of my ChildEventListener, the profile is the key in this situation so when I use ChildSnapshot.child("profile").getValue(User.class) it returns a null value. Also, (I'm not quite sure why this is) the value of the username was stored in a different class called User_message which was used to store the message. so my updated code looks something like this:
mDatabase.child("users").child(mUserId).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot ChildSnapshot, String s) {
User_message author = ChildSnapshot.getValue(User_message.class);
String author_username = author.getUsername();
I was facing the same problem and spent more than 5 hours. I added Default Constructor of the model and this solves my problem.
public class User {
public String email;
public String name;
public User() {
}
public User(String email, String name) {
this.email = email;
this.name = name;
}}
I hope this will help you. Thanks
I'm quickly realizing this is going to be an issue in Android with a lot of boilerplate and as I started refactoring my code I'm now effectively writing my own half-#ssed version of data binding. I don't want to spend more time generalizing it and re-inventing the wheel. I was wondering if there are any good solutions out there as 3rd party libraries that the community uses.
I've found robo-bindings and I really liked their presentation (focus on unit testing their own stuff, robustness, etc) but it seems like they remain quite small and I'm worried about issues with their library and general support/evolution going forward.
Any other libraries people are using?
Thanks.
Android M will provide powerful library for data binding!
It's available now in dev-preview version.
It looks amazing inside xml and java files:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}"
/>
Java bean:
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Binding:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
User user = new User("Test", "User");
binding.setUser(user);
}
I have an android application that reads an XML file and presents some information to the user. The data structure is a simple ArrayList. The user goes through the list and they may make changes or they may not make changes just depending. When they are done they save.
Eventually we want that data on the RDBMS but they may not have a data connection at that exact moment so I need to persist it. Even if they do have a data connection I would probably feel comfortable knowing I have the data structure serialized to the sd card.
So step one is do I persist it in a binary state (it is a very small data object) or serialize it out to XML which is what I was thinking at first. That way it can be uploaded to a web service in it's xml form.
If I serialize it out to XML are there any libraries / example I can read up on. I use SAX to parse it but was thinking there must be something like a simple overload to dump it out????
pseudo code:
ArrayList<MyOb>;
MyObj.Serialize("doc.xml");
If I persist it in it's binary state what are the steps I need to consider when the user does have a data connection and it is time ti upload.
TIA
JB
You can use a great library JAXB(Java Architecture for XML Binding) for
JAVA Beans <---> XML Data
JAXB Basic Tutorial
Here is some sample code to give you some taste of it
#XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
#XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
#XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
#XmlAttribute
public void setId(int id) {
this.id = id;
}
}
XML formed using this code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<name>mkyong</name>
</customer>
I'm going with persisting the data as xml on the sdcard. It's the best method from what I can discern.
I have an app which present some beaches to the users.there is a list view with the name of every beach and when the user press on a name it opens a new activity with a photo and some text.i have created a .java class for every beach( same copy-paste code ) and a common .xml file. is there any better way to do it?for example to have all the beaches and their text in a db?
why don't you just instance the same class but with different parameters in the constructor?
Something like this:
public class Beach{
protected String name;
protected String pathImage;
public Beach(String name, String pathImage){
this.name = name;
this.pathImage = pathImage;
}
}
//Somewhere else in your application...
Beach beach1 = new Beach("Cancun","/images/cancun.png");
Beach beach2 = new Beach("Miami","/images/miami.png");
I would store the beach information in a SQLlite database, then just create a single Beach view that knows how to display the information from the database. A problem with this is that you might want to build a simple tool to allow you to manage the information in the database so you don't have to do it through queries on the command line.
You could ofcourse create a (abstract) superclass Beach.java and let other beaches extend that class. That way, you've got less redundant code.
public abstract class Beach{
protected String name;
public Beach(String name){
this.name = name;
}
public abstract String getOtherInfo();
}
public class FirstBeach extends Beach{
public FirstBeach(){
super("FirstBeach");
}
public String getOtherInfo(){
return "someInfo";
}
}