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()
Related
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. :-)
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);
Just a real quick question. I've defined an array in one activity and I'm trying to access it in another one. I was planing to extend it as so:
public class MyChallenges extends Main {
But I need to extend 'ListActivity' to get my code to work, as I am using a custom listview, so I can't extend two activities. As shown:
public class MyChallenges extends ListActivity {
Is there any other way, apart from extending the main activity? This is the array I'm trying to access for read and write:
public String[][] arr = new String[2][6];
So, in short, how can I access the array 'arr' from another class? Thanks guys!
Make a class that contains the array, and reference that class from your activities. For example:
public class ArrayOfStuff
{
public static String[][] arr = new String[2][6];
}
Then, to modify the array, use something like ArrayOfStuff.arr[1][1] = "foo";
Happy coding!
The element of a resources file can only be used for single dimension arrays. In other words, everything between and is considered to be a single string.
If you want to store data in the way you describe (effectively pseudo-XML), you'll need to get the items as a single String[] using getStringArray(...) and parse the and elements yourself.
Personally I'd possibly go with a de-limited format such as...
Bahrain,12345
...then just use split(...).
Alternatively, define each as a JSONObject such as... and treat this array as JSON object and use JSON method for get value for element...
{name:Bahrain,codes:12345}
->simply create string array at res/values/strings.xml:
->after u can access this string array in whole application...
The problem at hand is that when i try to pull the value from the hashmap it is returning null.
I have a hashMap instance :
Map<Marker,Tag> theHashMap = new HashMap<Marker,Tag>();
where tag is a class that holds some simple information about the marker and the marker is a Google map marker.
I add to theHashMap at the start of the activity that this all happens in
theHashMap.put(mapController.AddMarker(new Tag(1, "City Of Dundee", DUNDEE_LOCATION, "untagged",), new Tag(1, "City Of Dundee", DUNDEE_LOCATION, "untagged",));
where mapController is a class that deals with everything googleMap related.
And mapController.AddMarker returns the marker that was added to the map.
after the hashMap is filled it is passed to the onMarkerClickListener for later reference.
I call
hashMap.get(marker); from within a marker listener where marker is the marker that was clicked.
It always returns null, I thought it may be because the hashMap inside the onMarkerClick listener was a separate instance but I tried making a pointer to the original and it didn't work, i also tried hashMap.get(marker.getTitle()); and with the marker.getID() thinking that it would compare there titles but it ended with the same result.
I'll add more information if requested but for now, is there any other way of taking the value from the hashMap based on the marker that was clicked?
In your code hashMap.put(marker,key); here in this hashMap you have used key -> custom obje ct(mapper).
Hence it returns null when you called by the key. where provide key (mapper) object should match with put (mapper) key.
You have to override equals and hashcode methods in Mapper class to solve this.
EDIT:
In HashMap your key is mapper object(custom object). As custom object(Mapper object) is key in HashMap we have to override equals and hashcode in Mapper class to fetch exact value.
If key as Primitives(int) or String Object, no need to do the above thing.
I want to make a deep copy method. I seeked help here the other day with this issue, but that was for a copy constructor. Now I need a regular method. I have the code created (nonworking), but I'm just not understanding it completely.
public GhostList deepCopy(){
int length=this.getLength();
GhostList jadeed=new GhostList();
Ghost[] data = new Ghost[length];
for (int i=0;i<this.getLength();i++){
data[i] = new Ghost();
data[i].setX(this.ghosts[i].getX());
data[i].setY(this.ghosts[i].getY());
data[i].setColor(this.ghosts[i].getColor());
data[i].setDirection(this.ghosts[i].getDirection());
}
return jadeed;
}
Now when I create a new GhostList named jadeed, and then under that I create a new data array of ghosts, does it know that data belongs to the jadeed GhostList? I dont see how the two can be associated, even though they should be.
Also, I'm not getting the lengths to match up for the copy and this.object. What is my problem?
You created a new GhostList and a new Ghost array.
You fill in the Ghost array and return the GhostList but the returned GhostList has nothing to do with the Ghost array.
You should add all the new ghosts to the GhostList
First, you mentioned a copy constructor. If you already have that working, then all you need to do in your deepCopy method is:
return new GhostList(this);
Let's forget that for now and get back to the code you posted. You are creating an array named data but you never used it anywhere. Aren't you supposed to assign this array to jadeed? Something like:
jadeed.ghosts = data;
And finally, instead of calling the method deepCopy, it would be better to call it clone and implement the Cloneable interface. Doing this allows everyone to know how to get a copy of your object using a standard interface.
Your GhostList class will have as its data member a reference to the array of Ghost. You've not shown us the class definition, so lets say that member is named foo. Now all you need to do is make the foo reference of the newly created jadeed object refer to the array of Ghost which you've created and populated. You can do it as:
jadeed.foo = data;
before you return jadeed.
If GhostList and everything it's composed of is Serializable, you can serialize the GhostList instance into a byte array and re-read it. It's a few lines of code, unless you use `Jakarta Commons Lang - one line of code:
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/SerializationUtils.html#clone%28java.io.Serializable%29