I have currently created a hash map in one class, as shown below:
public static HashMap BasicDetails;
which I want to access from another class.
I have also put the values from the same class where the HashMap was declared as so:
BasicDetails.put("currentPersonName", personName);
BasicDetails.put("currentPersonImage", personPhoto);
BasicDetails.put("currentPersonGooglePlusProfile", personGooglePlusProfile);
which takes retrieved Google account information and stores it into a hash map.
Now, in my second class, I want to access it. I did not know where to start so I declared a TextView variable known as nameValue
public TextView nameValue;
In my onCreate method, I then decided to equate this nameValue to the hash map equivalent value (currentPersonName):
nameValue = (TextView) findViewById(R.id.nameValue);
nameValue.setText(BasicDetails.get("currentPersonName"));
Yet the BasicDetails tag is highlighted in red, noting that it is not recognized by the class (despite the hash map being declared as public). I may also note that in the xml file I have created a blank textView object which has the id = nameValue as well.
Please help, any help would be much appreciated.
EDIT - Solution Found
I retyped the passing of the data to be passed via Intents, will post code shortly
Either make the information available by using statics (not recommended), use some kind of database (could be as simple as a text file) or pass an Intent along with your Activity. A nice tutorial on adding information to an Intent is found here: http://startandroid.ru/en/lessons/complete-list/241-lesson-28-extras-passing-data-using-intent.html
NameOfClassThatContainsMap.MapName.put()
Like this
Class A{
public static HashMap BasicDetails;
}
(in the other place):
A.BasicDetails.put("stuff);
Related
We are making a Firestore query by using collection name, so I thought to store collection name in a static class, or in the same class something like:
private static final String collUsers = "users";
and then use something like db.collections(collUsers).documents()
So is it safe to store collection names in fields?
Is it safe to store the collection name in the class of Firestore Project class file?
Sure, it is. The collection names are always Strings. You can use them directly in your reference:
db.collections("users").documents()
Or you can save that name in a variable. However, if you do that, that variable becomes a constant, as the name of the collection cannot be changed. So most probably the variable name might look like this:
public static final String USERS_COLLECTION = "users";
So the problem is not that you keep these names in your project file, the problem is how you read, write, and delete the data that exists there. In that case, you should always secure that data using Firestore Security Rules.
Yes, it's safe to keep your query for firestore open, until you have firestore rules in place which guard your data against any misuse and edits.
If you want to know more about securing your data: Read this
Given the following POJO example which stores local fields applicable only to the app running right here, nobody else whom also use the Firebase data:
#IgnoreExtraProperties
public class DispatchModel {
private String isotimestamp;
private String date_time;
private String event_uuid;
//...
private String locallyCreatedValue;
//...constructor, getters, setters
}
Given my Firebase data has the first three fields stored in it, and the locallyCreatedValue is added to my POJO during app runtime, is there a way to automatically fetch all the locally added content from a POJO instance and apply to the new instance when an update from onChildChanged event happens?
As it is right now, I'll have to manually obtain all the local field values and set them on the new instance:
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
DispatchModel newModel = dataSnapshot.getValue(DispatchModel.class);
// get list index of expiring instance
// get instance of old item at list index
// index = ...
// old = ...
// repeat this for every local item :-/
newModel.setLocallyCreatedValue(old.getLocallyCreatedValue);
dispatchList.set(index, newModel);
}
I plan on having quite a few local fields, is this my only option? Are there any functions Firebase offers that makes their automatic object instantiation more friendly to my extras? I'm not keen on creating distinct POJOs to track the Firebase POJOs in parallel. That lends to data errors from decoupled data updates and careful schedules for execution.
If all of your locally created values are expressed as standard getters and setters in your POJO, you are either going to have to copy them manually, or write some fairly intense Java reflection code to inspect the class, somehow figure out which properties are local (annotation? inclusion/exclusion from a known list?) and should be copied over, then actually do that work. You will not be able to get this "for free" using some utility (unless there happens to be some third party library that has solved this specific problem, and I doubt it).
Consider instead maybe storing your locally created values as a Map, then simply copying that map wholesale between objects. But then you have unknown types for the values if they're all different.
Or rewrite your code in JavaScript, which has easy property enumeration. :-)
Noob Java question here. I am wondering if there is a standard way to add additional/external properties to a POJO. i.e., say I have a User Object that I add to an ArrayList in my program. This Object will contain things like first name, last name, address, email, phone, etc., corresponding to whatever is defined in my database.
Now say that I have the requirement to include external attributes along with said User, such as Employer ID, Vehicle Plate #, Smartphone type. I will need to be able to include these extra properties when adding a User to my ArrayList - Is it possible to attach these strictly with Java so that I can have acces to them?
I've thought of going with something like a Value Object, in where the VO would include all the User Properties along with the extra fields to be added from outside Classes, but want to explore more possibilities. Any ideas? Thanks much
You have many possibilities. Here are a few that immediately spring to mind:
Refactor the User class. This is the obvious one, so I presume you have a good reason for not doing so.
Write a class that extends User, containing this information. Presuming you're only storing this information for a subset of the users that this information applies to, this makes the most sense.
Use composition - create a new class that holds a user instance and then any other information you want to add to it.
One big influence on what design you use will be whether all additional values are unique, or if a User can have multiple smartphones, vehicles, etc.
If all additional fields are all unique you can just slide in java.util.Properties (or other Maps). Your User class needs at least
import java.util.Properties;
class User
{
Properties extra_attr = new Properties();
// ... existing code ...
public void setExtraAttr(String field, String value) {
extra_attr.setProperty(field,value);
}
public String getExtraAttr(String field) {
return extra_attr.getProperty(field);
}
}
Then use calls like some_user.setExtraAttr("Employer ID","314159"); and some_user.getExtraAttr("Employer ID"); to set and get your extra attributes.
If you need multiplicity you may need a different approach, or you can just code over your implementation of Properties. For instance, you can rewrite setExtraAttr() to look for existing keys and add an index
public void setExtraAttr(String field, String value) {
if(extra_attr.getProperty(field) == null)
extra_attr.setProperty(field,value);
else {
int index = 1;
while(extra_attr.getProperty(field+index) != null)
index++;
extra_attr.setProperty(field+index,value);
}
}
You then need some kind of loop where ever you get attributes to look for and handle the multiple extra records.
I have a HashMap which takes in a String ID(ID of a chatroom) as a key and stores all the chats from that specific room in an arraylist as follow:
HashMap<String, ArrayList<ChatMessage>> chatHistoryHashMap = new HashMap<String, ArrayList<ChatMessage>>();
This is created in RoomActivity and I want to pass it to ChatActivity. How am I able to do this? I tried to make it a public static but using the "put" method in ChatActivity seems to do nothing, not sure why.
I also need this HashMap to be passed back to RoomActivity when the back button is pressed in ChatActivity.
Do I need to use Intents?
When you made anything static in your class then that variable is visible to all classes in that package so it cannot happen that you are not able to get the value in another activity.
To access the static variable you can either make th object of the class or you can directly access it by using function name.
You can also pass the object using intent.putExtra() and intent.getExtra()
I am trying to save two strings and a string array together in one package to an internal storage. But I am having an issue actually combining the three together as one. Here is my code:
class Save_Constructor extends Phone_Save{
String constructor_file_name;
String constructor_class_name;
String[] constructor_notes_to_attach;
Save_Constructor(){
constructor_file_name = file_name; constructor_class_name=name; constructor_notes_to_attach=text;
}
}
Ideally, I would like to compress these three elements into one, and return them back to the Phone_Save class to be saved into the Phones internal storage. Does anybody have an idea as to how to do this? Honestly I am not even sure whether or not to use a constructor for this, so I am open for any and all ideas. Thanks!
Just have a look at Java POJO/Bean class and try to understand it in detail. That is the solution for your problem. Just user setters and getters in your class and the use that Class's object as a single entity for your 3 different entities.
Just a quick google search and I got this for you.
Comment below if you dont understand anything.
Have a look at StringTokenizer Class. You have two ways to approach this. Either you put all the data into one String or into the StringArray. Iterate with for loop to put all this together and then call your store mothod.