Help a newb with creating a variable


Last Updated:

  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 (Text):
    1. public class MainActivity extends Activity {
    2.    
    3.     private int Item_Selected = 0;
    4.     /** Called when the activity is first created. */
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.        
    10.         GridView gridview = (GridView) findViewById(R.id.gridview);
    11.         gridview.setAdapter(new ImageAdapter(this));
    12.  
    13.         /** Called when the a pic is selected. */
    14.         gridview.setOnItemClickListener(new OnItemClickListener() {
    15.             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    16.                 Item_Selected = position;
    17.                 Toast.makeText(MainActivity.this, "" + Item_Selected, Toast.LENGTH_SHORT).show();
    18.                
    19.            
    20.            
    21.         Intent myIntent = new Intent(MainActivity.this, TabsActivity.class);
    22.         MainActivity.this.startActivity(myIntent);
    23.                        
    24.             }
    25.         });
    26.     }                  
    27.             }  
    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 (Text):
    1.  
    2. private int Item_Selected;
    3.     public class SyllabusTabActivity extends ListActivity {
    4.        
    5.  
    6.         /** Called when the activity is first created. */
    7.         @Override
    8.         public void onCreate(Bundle savedInstanceState) {
    9.           super.onCreate(savedInstanceState);
    10.           if (Item_Selected == 0) {
    11.               String[] Belt_Array = getResources().getStringArray(R.array.TanBelt_Array);
    12.               setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
    13.             }else {
    14.                 String[] Belt_Array = getResources().getStringArray(R.array.GrayBelt_Array);
    15.                   setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
    16.             }}  
    This is not working for me. How do i call upon that information?

    Advertisement
  2. scrapperstoo

    scrapperstoo Member

    1st problem is:

    Code (Text):
    1. private int Item_Selected = 0;
    which needs to be

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


    Then, in SyllabusTabActivity, get rid of

    Code (Text):
    1. int Item_Selected;
    and then replace

    Code (Text):
    1. if (Item_Selected == 0) {
    with

    Code (Text):
    1. if (MainActivity.Item_Selected == 0) {
    HTH :)
  3. alostpacket

    alostpacket Over Macho Grande? VIP Member

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

    2)
    Code (Text):
    1.  
    2. private static int itemSelected = 0;
    3.  
    4. public static int getItemSelected()
    5. {
    6.     return itemSelected;
    7. }
    8.  
    then to access it from other classes:

    Code (Text):
    1.  
    2. //solution 1
    3. int myLocalVariable = MainActivity.itemSelected;
    4.  
    5. //solution 2
    6. int myLocalVariable = MainActivity.getItemSelected();
    7.  
    8. //do some work
    9. if (myLocalVariable == 1)
    10. {
    11.     Log.d("MyAppTag", "hi");
    12. }
    13.  


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

    Code (Text):
    1.  
    2. Intent myIntent = new Intent(MainActivity.this, TabsActivity.class);
    3. myIntent.putExtra("current_item_selected", itemSelected);
    4. MainActivity.this.startActivity(myIntent);
    5.  
    then inside onCreate() in SyllabusTabActivity
    Code (Text):
    1.  
    2. //get the intent passed to the activity when it was created
    3. Intent myReceivedIntent = getIntent();
    4.  
    5. // get the int value out of the intent.  
    6. // -1 is the value we use if the int isnt found inside the intent
    7. int itemSelected = myReceivedIntet.getIntExtra ( "current_item_selected", -1);
    8.  
    9. if (itemSelected  == 0)
    10. {
    11.     //do some stuff
    12. }
    13.  
    scrapperstoo likes this.

Share This Page