Method of passing object to another activity [closed] - android

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have a RecyclerView filled with Car objects. A Car has a lot of variables like ID, size, colour, logo, etc. Every car is saved to the database.
Now, when I want to open the DetailActivity about the specific Car, should I pass the whole object into the new activity OR just an ID and get data from database in DetailActivity again?
Which solution will be more relevant and faster?

If you need to pass one or two properties then its fine. If it is more than two then it is preferred if you use id and query it from DB.

First you should make your object implement Parcelable.
Once you have your object implemented the Parcelable, you attach it to the Intent like this:
Intent intent = new Intent();
intent.putExtra("extra_object_1", your_parcelable_object);
When you are ready to pull the object, use intent.getParcelableExtra():
Intent intent = Context.getIntent();
MyParcelable obj = (MyParcelable) intent.getParcelableExtra("extra_object_1");

If object size is small, you should pass the data through the intent using Parcelable. if data size is large then pass the id to you activity and then get the data from database.
Based on this Answer you can pass 1 MB of data in Intent.
So choose the approach based on the situation.

Related

How do I see all my data that has been read in Cloud Firestore? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have successfully read all my user data into cloud firestore but unable to see all my data in one go.
As you can see in the screen capture, "Sara" has been read. Beforehand, "Mira" and "Kiena" has also been read.
How do I see "Mira" and "Kiena" together with "Sara" in one screen? I want to see all the data that has been read in Cloud Firestore.
I need to have all this as a record for my thesis.
I set the user data with this code:
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("sampleData/userdata");
You need to add this
SetOptions.merge()
inside the set function in your code
what is happening now is that firestore takes a key = name and values = sara/mira/etc and keeps overriding the values since the key is the same, but when you use the merge options it will not override and will display all in a list
It looks like you're setting the user data with something like:
db.collection("sampleData").document("userData").set(...)
Any time you execute this code, you're overwriting whatever data already exists in userData.
If you want to create a new document each time you run the code, do:
db.collection("sampleData").add(...)
This will generate a new document with a unique ID each time you execute it.
If the document needs to be associated with the current user from Firebase Authentication, you'll want to use the user's UID as the document name. That'd look like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("sampleData").document(uid).set(...)

Sorting a list with integers android [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Should I use the comparable or comparator when I want to sort my list based on my own personal object?
should I use the collection operation in my recycler view adapter?
Is it a list of integers (as you specify in your title)?
you can use Arrays#sort
Arrays.sort(someArray, new Comparable<Integer>() {
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
});
If it's just a list of integers, and they are in for instance a List object, you don't have to use a comparator, unless you need to parse the integers as something other than plain numbers.
Collections.sort(myList);
If it's one of your own objects, look at Tyler Sebastians answer. Collections comes with a similar method for lists.
Collections.sort(myList, new MyComparator());

When Change any fragment data state remain as is no reload data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do i swapping one fragment to another fragment so enter data as remain means i have one fragment in that one search functionality.
in that user can entered different keyword by search but it navigate from one to other fragment that data is erased i want that entered data remain as it is.
is it possible if yes than how ?
Your problem is not very clear...
Passing arguments from a fragment to another can be done with a Bundle.
I give you an example.
Store the data your user type in a String and put it in a Bundle like this:
Bundle bundle = new Bundle();
bundle.putString("NAME OF YOUR DATA", "VALUE OF YOUR DATA");
new YourFragmentClass().setArguments(bundle);
Then on your second fragment, get your data this way:
Bundle data = getArguments();
data.getString("NAME OF YOUR DATA","DEFAULT VALUE IF THIS DOESNT EXIST")
You can then keep your data from a fragment to another.
If it doesnt fit to your needs, have a look on the SharedPreferences

Place data from jsonobject into AutoCompleteTextview [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to get data from http://schedule.sumdu.edu.ua/index/json?method=getTeachers, parse it and load into AutoCompleteTextview. Any suggestions?
You need to access the service URL using an AsyncTask<>, then later on get its response into a String object.
And, parse it, using JSONObject/Json Array present in android. You will get many examples for this.
Later on you can create a String array, load your data in it, and set it for auto complete for text view.
Here is an example for this.
String[] listTeachers; // initialize this with teachers JSON data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.layout_teacher_list, listTeachers);
tvTeacher.setAdapter(adapter);

What is a concrete example of Android app functionality that uses HashMap? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in Android, and understand only very basically that HashMap class enables key/value pairs. But how does this translate into actually using this in an Android app? Could someone provide a simple, plain English example of what case you might want to use HashMap in an app? I cannot imagine a case where I might need it. Make up an Android app idea, if needed. Thanks in advance.
I am looking for a "big picture" analysis that will give some examples where you might use HashMap with certain Android functionalities you are trying to implement.
HashMap or Map interface is not new on android, This is Java Collections framework.
Java collection are meant to be used in several cases to hold data and contain 3 interfaces:
List - Basically simple list,or linked list implementations
Set - The same as list but won't hold 2 equal obejcts(You need to implement you own equals and hashcode)
Map - as you said key value pair.
Uses:
List - For anything, just to hold data
Set - For list of data that we want that all of the items will be unique.
Map - Key value and the most common example is the use for DB items, or something with ids.. for example:
bookId, Book.. I that case you can take the object by id.. This is the most common
I attached link for Java collection tutorial.. It is very important framework that you have to know if you are going to develop java/android
http://tutorials.jenkov.com/java-collections/index.html
Hope that helps
We could use HashMap to keep a list of employess together with their respective salaries.
We can do:
HashMap<String, Float> emplMap = new HashMap<String, Float>();
emplMap.put("fred", 1.000);
for(String name : emplMap.keySet()) {
System.out.print(name + "'s salary is" + emplMap.get(name));
}
Should print
"fred's salary 1.000"

Categories

Resources