Help a newb with creating a variable


  1. igorski88

    igorski88 New Member

    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?

    Advertisement
  2. scrapperstoo

    scrapperstoo Member

    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 :)
  3. alostpacket

    alostpacket Over Macho Grande? VIP Member

    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
    }
    
    scrapperstoo likes this.

Share This Page