Solved

Help Passing Information

Posted on 2011-10-01
6
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of Part-1 as evident from the title. Please visit this page (http://www.experts-exchange.com/articles/18706/Backing-up-personal-data-on-your-Android-device-Part-1.html) to read the earlier Part -1. If y…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

864 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now