Solved

Help Passing Information

Posted on 2011-10-01
6
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RTSP blocked by AT&T? 11 70
spam sms 9 50
telnet / ping for Android Mobile. 4 81
Contacts > exchange syncing to Android for contacts  and vice versa 6 32
If your app took Google’s lash recently, here are the 5 most likely reasons.
These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
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.
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…

710 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