I am a beginner in android studio, so please bear with me.
I've retrieved data from my intent in a recyclerview activity but I don't know how to pass it to its corresponding adapter class (recyclerview_adapter.java).
How should I start, does anybody have an example?
Thanks in advance.
you can just create a constructor inside adapter class with variable you want to pass and place a global variable by which you can use that variable(data).
class adapter{
// you can pass whole object or any kind of data here as well, just replace string with appropriate class name...
private String data;
// the data string will be set from your recycle activity...
public adapter(String data){
this.data = data;
}
// use the data string anywhere in adapter class...
}
Related
In my application whenever we click the "SEARCH" button it makes Http call and get the json as response. I parsed the json and save the data in arraylist bdata.
ArrayList bdata = new ArrayList();
BusData contain getters and setters . I also used one adapter class for custom listview.
now i want to set the adapter to listview but the listview is in another class (not in the class where search button is locate) so how to send my arraylist from one activity to another activity with the help of intents for set the adapter to listview.
Thank you.
A way to do this is by serializing your arraylist to a JSON.
Use GSON library
compile 'com.google.code.gson:gson:2.3.1'
Add the above line in your build.gradle file.
Make a class to help you serializing your objects
public class SerializationHelper {
private static Gson gson = new Gson();
public static String serialize(Object object) {
return gson.toJson(object);
}
public static Object deserialize(String json, Class classType) {
return gson.fromJson(json, classType);
}
}
Now when you want to start the new activity add the serialized string.
String json=SerializationHelper.serialize(myArrayList);
intent.putExtra("data",json);
And in your new activity on create get it and create your object again.
String json=intent.getStringExtra("data");
Object deserializedObject=SerializationHelper.deserialize(json,ArrayList.class);
Now cast your object!
ArrayList<MyClass> myCoolArray=(ArrayList<MyClass>)deserializedObject.
Other simpler way is to make your arraylist static and public, and store it in an other class.
public class GlobalStuff{
public static ArrayList<MyClass> myAwesomeList;
}
Now acces your list by GlobalStuff.myAwesomeList.
You need to create custom class (ArrayList<data class>) to Parcelable and then you can pass it to other activity.
http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html
When passing data through activities you should use Parcelable, the are many examples in stackoverflow, this one is good:
https://stackoverflow.com/a/22446641/2091315
Next, you need to configure the intent, on the "sender" Activity:
Intent intent = new Intent(getApplicationContext(),
ReceiverActivity.class);
intent.putExtra("arrayListIdentifier",parcelableArrayClass);
startActivity(intent);
On the "receiver" Activity:
parcelableArrayClass myParcelableObject = (parcelableArrayClass) getIntent().getParcelableExtra("arrayListIdentifier");
what i have is three activities, the first one called penddingorders which i make an array list inside it and i add strings to it like this :
ArrayList prodlist=new ArrayList();
for(int z=0;z<=mylist.size();z++){
if(id.equals(mylist.get(arg2).getOrderId()))
{
product=mylist.get(arg2).getProductName();
quantity=mylist.get(arg2).getQuantity();
unit=mylist.get(arg2).getUnit();
price=mylist.get(arg2).getPrice();
totalprice=mylist.get(arg2).getTotalPrice();
totalpriceAfterDiscount=mylist.get(arg2).getPriceAfterDiscount();
note=mylist.get(arg2).getNote();
prodlist.add(product);
prodlist.add(quantity);
prodlist.add(unit);
prodlist.add(totalprice);
prodlist.add(price);
prodlist.add(totalpriceAfterDiscount);
prodlist.add(note);
arg2++;
}
and i have print it in the logcat and it shows correctly:
Log.e("product list",prodlist+"");
and i have send it from the first activity to the third activity like this , though i should pass through the second one before i go to the third one :
Intent intent = new Intent(PenddingOrders.this, ProductInfoDetails.class);
intent.putStringArrayListExtra("prodlist", prodlist);
startActivity(intent);
and in the third activity i get the array list through this code :
try{
prodlist = getIntent().getStringArrayListExtra("prodlist");
}
catch(Exception e){
e.printStackTrace();
}
Log.e("prodlist",prodlist+"");
but in the logcat i get null value .. so what i am doing wrong please help ??
you can set your property using getter setter so you can manage your value very well.
and make it parceable. so you can use it Extras ArrayList of it into next Activity.
Please follow this https://stackoverflow.com/a/15731120/942224
This is my ans on stackoverflow please checked and hope it's help alot:
Java / Android ArrayList in different Activity
Better to use Android Application class to persist data.
For reference link
public class MyApplication extends Application {
private ArrayList<String> list;
public void setValue(ArrayList<String> list) {
this.list = list;
}
public ArrayList<String> getValue() {
return list;
}
}
Step-1
First set the value from your Activity
MyApplication globalVariable = (MyApplication) getApplicationContext();
globalVariable.setValue(prodlist)// Set your ArraList that you want to store
Step-2
Retrieve the value in other activity like
MyApplication globalVariable = (MyApplication) getApplicationContext();
// Get ArrayList from global/application context
ArrayList<String> list = globalVariable.getValue();
and don't forget to register in Manifest.xml
<application
android:name="YOUR_PACKAGE_NAME.MyApplication "
-----------------------
I have a problem with with sharing data between two different activities. I have data like :
int number
String name
int number_2
int time
int total
I'm trying to make something like order list with this set of data . So it will take one set of data , then back to previous activity , move forward and again add data to it .
I have an idea of making it in array of object - but data inside was cleared after changing activity.
How can I make it ?
I don't know if and how to add Array of object to SharedPreferences , and get value of one element from there.
You should have a look at the documentation of the Intent(s) if you want to do that on the fly associating a key to the value(s) that you want to pass to your second activity.
Anyway, you can think any(sharedpref, database,...) way to pass your parameters but for those kind of things it's a convention and a good practice to follow that.
Don't used share preferences for this...Use the singleton pattern, extend Application, or just make a class with static variables and update them...
You can use .putExtra but since you are communicating with more than one activity the above suggestions are probably the best.
public class ShareData {
private String s;
private int s;
private static ShareData shareData = new ShareData();
private ShareData(){}
public static ShareData getInstance(){ return shareData}
//create getters and setters;
}
Why not to use Intents
Intent intent = new Intent(FirstActivity.this, (destination activity)SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);
in the second activity
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("some_key");
String value2 = bundle.getString("some_other_key");
EDIT if you want to read more about adding array to shared preferences check this
Is it possible to add an array or object to SharedPreferences on Android
also this
http://www.sherif.mobi/2012/05/string-arrays-and-object-arrays-in.html
I'm trying to change Roman's Nurik WizardPager so that in one of the steps display some data from my database.
I'm taking a Model and an UI from the library and I modify them so I can have a
DisplayOrderPage with a parameter ArrayList for my data
public DisplayOrderPage(ModelCallbacks callbacks, ArrayList<Salads> ord , String title) {
super(callbacks, ord, title);
}
and a DisplayOrderFragment which is going to display the data.
I can get an ArrayList with my data from my database in the MainActivity but I don't know how to pass that data to the SandwichWizardModel since it's not an Activity.
you can do one thing.. also pass the context of the activity as parameter. And declare a method in the activity which change the gui within a handler .. like this
in Activity
changeGUI(){
new Handler().post(new Runnable(){
// change gui
}
}
and call like
methodInCLasse(this, arrayList);
I have a method called getData() in an Activity that returns a String[]:
Example:
public class Strings{
public String[] getData(){
String[] data = {"one","two","three"};
return data;
}
}
My question is how to retrieve these items, because in another activity I want to put this into a SimpleAdapter list. I can call
String[] data = Strings.getData();
then have that as a variable that is technically the array, but I need to read out the items into the adapter. I know its probably simple but like I said, noob, haha.
For sharing data between different activity, I think you need to send that data with Intent before you change Activity
intent.putExtra("TheStrings", this.getStrings());