Solved

Help Passing Information

Posted on 2011-10-01
6
193 Views
Last Modified: 2012-05-12
I'm a new Android programmer. I need help passing an array to a new activity.

I've eliminated some code for brevity:

 
//this is my array
private ArrayList<InvestmentInfo> investmentData = new ArrayList<InvestmentInfo>();

//these are what the objects in my array look like
public class InvestmentInfo {
    	private int monthNumber;
    	private String monthlyInterestPayment;
    	
    	public InvestmentInfo(int month, String payment) {
    		this.monthNumber = month;
    		this.monthlyInterestPayment = payment;   		
    	}
    }

Open in new window


What I am trying to do is pass this array onto a new screen (activity) so I can write code to format the contents in a grid view. I have read about Parcelable, and passing as strings, but I haven't found an explanation that I could understand.
0
Comment
Question by:InquisitiveProgrammer
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:Gyufa
ID: 36897860
Hi!

Best solution is to write your own Application class, and store your shared objects in a HashMap there. In Activities you can call ((YourApplication) getApplication).getSharedMap and transfer objects via this map. It's "not typesafe" but is working 100% OK. You can use static variables as well, or singletons, but their behavior is quite unpredictable.

I hope it's helping,

Gyufa
0
 
LVL 1

Expert Comment

by:Gyufa
ID: 36897899
By the way I'm using a HashMap<String, Object>, which is particularly useful if you have multiple objects that need to be passed to an other activity. But in this case you have to cast your objects.
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36897980
The following code is what I'm using:

 
InvestmentApplication application = (InvestmentApplication) getApplication();
application.setInvestmentAmount(investment);
application.setInterestPercentage(interestPercent);
application.setMonths(mths);
Intent intent = new Intent(InvestmentCalculatorActivity.this,DisplayData.class);
startActivity(intent);

Open in new window


Then in my second activity I'm attempting to get the information like so:

 
InvestmentApplication application = (InvestmentApplication) getApplication();
double amt = application.getInvestmentAmount();
double percentRate = application.getInterestPercentage();
int months = application.getMonths();

Open in new window


I'm not getting any errors in my code, but it's crashing at runtime.

Suggestions?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36897983
DisplayData.class is the java file where I'm "passing" the information on to.
0
 
LVL 1

Accepted Solution

by:
Gyufa earned 500 total points
ID: 36898060
Did you get a NullPointerException? You should check your AndroidManifest.xml file, so if you set up the <application> tag correctly. There is an attribute called "android:name", where you should define your InvestmentApplication class, just like the Activites.

http://developer.android.com/guide/topics/manifest/application-element.html
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36898099
Thanks Gyufa, I looked and the name property wasn't set for the application tag in the AndroidManifest.xml file.

I'll have more questions this evening, so keep a look out if you want some points! :-)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Callkit and Android comparison 4 43
Add mobile access to browser application 3 193
Plex Plugins question 9 69
Need a good Angular tutorial 5 81
First off, some of you reading this are already thinking there is an error in the title. It really should be Kindle Fire versus OTHER Android tablets. That's because the Amazon Kindle Fire IS an Android tablet. Well, it is and it's not. It's running…
A short article about problems I had with the new location API and permissions in Marshmallow
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question