Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old March 3rd, 2011, 02:00 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Dec 2009
Posts: 4
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default Help a newb with creating a variable

So here what im trying to do. Im trying to set a variable that i can bring up later in another Activity but in the same packege.

Code:
public class MainActivity extends Activity {
	
	private int Item_Selected = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        /** Called when the a pic is selected. */
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	Item_Selected = position;
                Toast.makeText(MainActivity.this, "" + Item_Selected, Toast.LENGTH_SHORT).show();
                
        	
        	
        Intent myIntent = new Intent(MainActivity.this, TabsActivity.class);
    	MainActivity.this.startActivity(myIntent);
                		
            }
        });
    }               	
        	}
SO I created the Variable and even tested in in the toast to make sure it works. Now I need to call on that variables information in the following code
Code:
private int Item_Selected;
	public class SyllabusTabActivity extends ListActivity {
	   

		/** Called when the activity is first created. */
		@Override
		public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  if (Item_Selected == 0) {
			  String[] Belt_Array = getResources().getStringArray(R.array.TanBelt_Array);
			  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
			}else {
				String[] Belt_Array = getResources().getStringArray(R.array.GrayBelt_Array);
				  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
			}}
This is not working for me. How do i call upon that information?

igorski88 is offline  
Reply With Quote
sponsored links
Old March 4th, 2011, 03:19 PM   #2 (permalink)
New Member
 
Join Date: Dec 2010
Posts: 9
 
Device(s):
Carrier: Not Provided

Thanks: 3
Thanked 0 Times in 0 Posts
Default

1st problem is:

Code:
private int Item_Selected = 0;
which needs to be

Code:
public int Item_Selected = 0;
if you want to be able to access it from another class.


Then, in SyllabusTabActivity, get rid of

Code:
int Item_Selected;
and then replace

Code:
if (Item_Selected == 0) {
with

Code:
if (MainActivity.Item_Selected == 0) {
HTH
scrapperstoo is offline  
Reply With Quote
Old March 4th, 2011, 08:50 PM   #3 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,873
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,582
Thanked 3,563 Times in 1,522 Posts
Default

it needs to be static also
1)
Code:
public static int itemSelected = 0;
or he could leave it private and use a getter

2)
Code:
private static int itemSelected = 0;

public static int getItemSelected()
{
    return itemSelected;
}
then to access it from other classes:

Code:
//solution 1
int myLocalVariable = MainActivity.itemSelected; 

//solution 2
int myLocalVariable = MainActivity.getItemSelected();

//do some work
if (myLocalVariable == 1)
{
    Log.d("MyAppTag", "hi");
}


But the "best practice" way to send data would be in the intent

Code:
Intent myIntent = new Intent(MainActivity.this, TabsActivity.class);
myIntent.putExtra("current_item_selected", itemSelected);
MainActivity.this.startActivity(myIntent);
then inside onCreate() in SyllabusTabActivity
Code:
//get the intent passed to the activity when it was created
Intent myReceivedIntent = getIntent();

// get the int value out of the intent.  
// -1 is the value we use if the int isnt found inside the intent
int itemSelected = myReceivedIntet.getIntExtra ( "current_item_selected", -1);

if (itemSelected  == 0) 
{
    //do some stuff
}
alostpacket is offline  
Reply With Quote
The Following User Says Thank You to alostpacket For This Useful Post:
scrapperstoo (March 5th, 2011)
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:03 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.