Solved

Help Passing Information

Posted on 2011-10-01
6
195 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
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…

740 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