Solved

Help Passing Information

Posted on 2011-10-01
6
190 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Recently Amazon announced their entry in the tablet device called the Amazon Kindle Fire. Some of you might have used an Ipad or have considered buying one. While they're similar in form and function in that they both have color displays with multit…
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.
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now