How to put data to spinner on another layout? - android

How can I to put data to spinner which is on another layout?
I am newby on android, and now I have problem with spinners.
I have data at first class with his layout. And I want to put that data to other spinner which is on another layout. What I need to do? Create new Class or What?
Please help! :)
Data is- Array

You should put your data where it is accessible from everywhere.
so please put your data in database using SQLite or ORMs that help you work with Native database like SugarORM, ORMLite and Realm.
another way is to apply singleton pattern to store your data in a different class.

You said you have an Array
Then make Array static
eg:- public static ArrayList<> new_array = new ArrayList<>;
now call this array any where in the activity
you will get data

there is many to manage array data.
- create arraylist and use that data anywhere in the activity.
- using database also you can user.
- using Read & writing arrays of Parcelable objects
- using global application class ....etc

Related

Xamarin listview with objects

If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.
You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.

"global" array with different data types - Android

I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.

Using multiple static ArrayLists through a whole Android application

I am developing my first android Application and hoping to get some tips here.
I am getting a JSONObject from an url which then will be parsed in an ArrayList<MyObject>. The list will be used in multiple tabs and be filtered as needed for the tabs. The objects within these list can be modified by the user and the changes should be synchronized with the lists.
So, to speed up loading time I have created a class DataHolder as a singleton which contains 7 arraylist, based from the one JSONObject in different sorting order and filter criterion. The objects in these lists are references from the original list. Populating the lists works fine.
The lists will be used in different fragments and activities.
Now the problem: the second activity contains tabs with fragments. After initializing the fragment... all arraylists in the DataHolder counts 0! I have to save the JSONObject in SharedPreferences and populate it again to get the List. I can't load the url again because it is slowing down the app to much and using SharedPreferences is not an option (I think) because of the need to synchronized the Lists. I have read that using static variables is not the optimal solution, but it seems to be the easiest way :(
What can I do to solve this problem? Should I use Parcelable Objects and always pass the Lists around? Or maybe use SQLite? Or are there other approaches?
The SQLite way is definitively the correct approach, imho.
You should use the internal database to store such lists :
http://developer.android.com/guide/topics/data/data-storage.html#db
use static list for local operation .when user comes out of that screen(activity) save that changes into database.

How to save parsed data in android?

I am creating an android application in which I am parsing so much XML data (XML data comes from a server) which contains Strings and image url's also.I need to use this data in many part of application. So for saving all these data I have used ArrayList and HashMap.I have declared ArrayList's and HashMap's variables in a single class as public static variables so I can access this data through a single class whenever I need.
And for images I have created ArrayList and placed in same single class same as other data(public static).Once I download a image through image url's I save those image drawables to these ArrayList variables so whenever I need any image again so I use these variables to get it.
Now my doubt is whether this approach is right or not. Please suggest me the right way.
Thank you
You should actually take a look at: http://developer.android.com/guide/topics/data/data-storage.html
It all depends on how you use the data, does it need to be updated all the time when the user opens the app you could take a look at this answer: How to declare global variables in Android? where you declare a global application and have the variables there.
Otherwise I would actually advice to use an sqlite database. http://developer.android.com/guide/topics/data/data-storage.html#db One good example is here: http://www.vogella.de/articles/AndroidSQLite/article.html

Android Development - Using an ArrayList<String> vs an array of strings with ArrayAdapter

I am new to Android development, and I had a question about an excerpt I read in Commonsware's "The Busy Coder's Guide to Android Development". There is an example where it is constructing a class extending ListActivity, and showing the ability to remove and delete from the list via Context Menus. Prior to setting up an ArrayAdapter with the source of strings for the list, the text says to convert the String array to an ArrayList type, because "we want to be able to change the contents of the list on the fly, and that is much easier if you use an ArrayList rather than a ordinary String array"
I understand this concept IF we were using the ArrayList or String array to manipulate the list directly, but why does that matter if we're using the ArrayAdapter.add/insert/remove methods? Once the ArrayAdapter is created (and from what I understand from that .java file), it turns the source data into a List object whether it is coming from an array or ArrayList.
I tried using the normal array when creating the adapter and I got an error (as expected by what the text said), but I do not understand why. What is the difference between creating an ArrayAdapter with a string array vs an ArrayList...won't it turn it into the same List object anyway? Is there some kind of tie back to the original source variable when trying to add/insert/remove via the adapter?
Sorry for the long question, but thanks in advance.
You can change the original array/arraylist. Then you call notifyDataSetChanged() on the adapter and it updates the list with the new information. If you use an ArrayList, then you can use all of its normal operations to insert/remove data at an arbitary index that you cannot do with an ordinary array of Strings.
So the contents of the ListView is very much tied back to the original data source- the contents isn't fixed by the adapter at the time you call myListView.setAdapter().
However, the add, remove, insert etc. methods will cause an error if you try them with an array because the array can't be used like this. The ArrayAdapter will actually convert the String array into an AbstractList object, but which can't be modified.

Categories

Resources