I want to pass Object of one activity to another but i am not able to do that.The problem is the eclipse is putting the Object which i want to pass in red clause.This is the code i am using:
DatabaseHandler1 db1 = new DatabaseHandler1(SecureMessagesActivity.this);
Intent intent = new Intent(SecureMessagesActivity.this,ShowTheFoldersInSdCard.class);
intent.putExtra("id",(int)id);
intent.putExtra("MyClassObject", db1);
In the last line,it is showing problem in db1.Please help.Thanks in advance.
Send only id through intent like this
Intent intent = new Intent(SecureMessagesActivity.this,ShowTheFoldersInSdCard.class);
intent.putExtra("id",(int)id);
And in another activity create Helper object like this
DatabaseHandler1 db1 = new DatabaseHandler1(SecondActivity.this);
If you want to have only one instance of your Helper through whole Application, i think you should create class extending Application and create Helper object there like this
DatabaseHandler1 db1 = new DatabaseHandler1(YourApplicationClass.this);
You have to make it public and static. Then in every Activity you can call it like this
YourApplicationClass.db1.doSomething();
Related
I am trying to pass list to next activity but its giving me classcast exception how can i solve it?I am implementing serializable in model class.
Someone help me in resolving the issue.
List<PagesSqliteData>pagesSqliteDataArrayList = new ArrayList<>();
PagesSqliteData pagesSqlite = new PagesSqliteData();
pagesSqlite.setToc_name(bookingsSqliteDataArrayList.get(position).getToc_name());
pagesSqlite.setCompletion(bookingsSqliteDataArrayList.get(position).getCompletion());
pagesSqlite.setCategory_id(bookingsSqliteDataArrayList.get(position).getCategory_id());
pagesSqlite.setBooking_id(bookingsSqliteDataArrayList.get(position).getBooking_id());
pagesSqlite.setTable_of_content_id(bookingsSqliteDataArrayList.get(position).getTable_of_content_id());
pagesSqliteDataArrayList.add(pagesSqlite);
Intent i = new Intent(MyBookings.this, Pages.class);
i.putExtra("LIST",pagesSqliteDataArrayList);
startActivity(i);
}
Fetching like this in next activity:
PagesSqliteData details = (PagesSqliteData)getIntent().getSerializableExtra("LIST");
You get a ClassCastException for one simple reason. Look at this commented version of your code:
List<PagesSqliteData>pagesSqliteDataArrayList = new ArrayList<>();//HERE YOU CREATE AN ARRAYLIST!!!
PagesSqliteData pagesSqlite = new PagesSqliteData();
pagesSqlite.setToc_name(bookingsSqliteDataArrayList.get(position).getToc_name());
pagesSqlite.setCompletion(bookingsSqliteDataArrayList.get(position).getCompletion());
pagesSqlite.setCategory_id(bookingsSqliteDataArrayList.get(position).getCategory_id());
pagesSqlite.setBooking_id(bookingsSqliteDataArrayList.get(position).getBooking_id());
pagesSqlite.setTable_of_content_id(bookingsSqliteDataArrayList.get(position).getTable_of_content_id());
pagesSqliteDataArrayList.add(pagesSqlite);
Intent i = new Intent(MyBookings.this, Pages.class);
i.putExtra("LIST",pagesSqliteDataArrayList);
startActivity(i);
And when you load:
PagesSqliteData details = (PagesSqliteData)getIntent().getSerializableExtra("LIST");//HERE IT IS SUDDENLY A SINGLE OBJECT
So when you save, ignore the arrayList. Pass pagesSqlite instead. And make sure this class is either Serializable or Parcelable. Or when you load, load it into an ArrayList. What you do is equivalent to:
PagesSqliteData data = new ArrayList<PagesSqliteData>();
And that is something you can't do.
I am trying to send this object information to another activity with intent. All information is in this picture.
First Activity
Intent intent = new Intent(getApplicationContext(),AnotherActivity.class);
intent.putSerializable("VALUE", listItem);
Second Activity
Intent intent=this.getIntent();
Object list = intent.getSerializable("VALUE");
Please change Object to GeoLabStudent
you should use adapter's reference to get item
GeoLabStudent object = (GeoLabStudent)geoLabAdapter.getItem(position);
Make your model GeoLabStudent to serializable or parcelable for sending your object to other activity
Example
I am trying to send an array of objects to a new intent, but the problem is that I don't know how to send the whole array at once. The method I managed to get it working is adding object by object, in a for loop, and in the second activity I have to get them one by one, something like this:
Main activity:
Intent newIntent = new Intent(mainA.this, secondA.class);
for(int i=0;i<numberOfObjects;i++)
newIntent.putExtra("object"+i,myObjects[i]);
newIntent.putExtra("length", x);
startActivity(newIntent);
Second activity:
Serializable n = getIntent().getSerializableExtra("length");
int x = Integer.parseInt(n.toString());
myObjects = new objClass[x];
for(int i=0;i<x;i++)
myObjects[i] = (objClass) getIntent().getSerializableExtra("object"+i);
Even if this method works, and gets me the right results, isn't there a better/faster/cleaner solution?(I searched a lot, but haven't found a better way, maybe I don't know what exactly to look for).
Make your object serializable and put all objects in a Arraylist and send it.ArrayList itself implements serializable.
Intent newIntent = new Intent(mainA.this, secondA.class);
newIntent.putExtra("list",listOfObjets);
startActivity(newIntent);
Second Activity:
ArrayList<YourObjects> list = (ArrayList<YourObjects>)getIntent().getSerializableExtra("list");
Hi you can try this as well.. Create setter getter class which will set and get your object. next create the array list of your setter getter class.next add all objects into arraylist using set method in for loop. next add that arraylist object into bundle using setArguments.
Same you can access that bundle using getArguments that's it.
I have a fragment which summons a custom CursorAdaper and display it on a ListView
. The thing is I want to change the cursor by changeCursor() from another activity when I add new data, How can I get access to the CursorAdapter displayed on the fragment?
Essentially you have to pass data from one Activity to another and let the fragment of you choice receive the data (each fragment of a given Activity may get the Intent that started/restarted/resumed the Activity).
Consider this code
-- pass data:
String[] myListEntries = getNewListContents();
Intent updateList = new Intent(this, ActivityThatListFragmentBelongsTo.class);
updateList.putExtra("updated_list", myListEntries);
startActivity(updateList);
-- receive data (in fragment):
#Override
public void onResume() {
Intent wasStartedWithData = getActivity().getIntent();
String[] updatedList = wasStartedWithData.getStringArrayExtra("updated_list");
// pass updatedList to adapter
}
Now you might actually have more complicated data than an Array of Strings. In this case
you could create a class that implements 'Parcelable' (http://developer.android.com/reference/android/os/Parcelable.html) and call
putExtra(Parcelable parcel) / getParcelableExtra(String name) on the Intent.
I have this application with 2 classes, in the first class I define a list array wich I want to access in my second class, how do I do that? class one (with the array extends listActivity and the other class extends Activity). I don't think it's nessecary to post my code as I believe there is a quick solution to this I just don't know it. Allthough I can post it if you can't help me without seeing the actual code.
You can use a custom ApplicationContext to share global state between activities - see here for more details...
you can pass ArrayList<String> 's across activities using intentExtras.
ArrayList<String> myArrayList = new ArrayList<String>();
// populate your array -> if you want to send objects make a string form of them
Intent myIntent = new Intent(this, Activit2.class);
mtIntent.putStringArrayList("my_array_list", myArrayList);
In Activity2
Intent intent = getIntent();
List<String> stringMediaList = intent.getStringArrayListExtra("my_array_list");
for (int i = 0; i < stringMediaList.size(); i++) {
//reconstruct object if needed
}
this will work if you dont want to use parcelable