Help a newb with creating a variable

Put your problem here if it does not fit any of the other categories.

Help a newb with creating a variable

Postby igorski88 » Thu Mar 03, 2011 8:58 pm

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.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  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.                 }  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private int Item_Selected;
  2.         public class SyllabusTabActivity extends ListActivity {
  3.            
  4.  
  5.                 /** Called when the activity is first created. */
  6.                 @Override
  7.                 public void onCreate(Bundle savedInstanceState) {
  8.                   super.onCreate(savedInstanceState);
  9.                   if (Item_Selected == 0) {
  10.                           String[] Belt_Array = getResources().getStringArray(R.array.TanBelt_Array);
  11.                           setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
  12.                         }else {
  13.                                 String[] Belt_Array = getResources().getStringArray(R.array.GrayBelt_Array);
  14.                                   setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, Belt_Array));
  15.                         }}  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This is not working for me. How do i call upon that information?
igorski88
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 27, 2011 9:03 pm

Top

Re: Help a newb with creating a variable

Postby strider2023 » Thu Mar 03, 2011 10:15 pm

One make change the declaration of Item_Selected to "public static int Item_Selected" in your first class where you will be setting the value. You can recover this value in any other class by simply by writing the following "if(<ClassName>.Item_Selected == 0)".
Two use shared preferences but I dont think you will need that in this case.
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Help a newb with creating a variable

Postby mitutitu » Thu Mar 03, 2011 10:26 pm

Hello igorski88,
If I'm not wrong then you're trying to pass a value which is stored in a variable from one activity to another. So in that case you can pass it along with the intent which is used in startActivity() method. Here how it is:

in the first activity, from which you are calling another activity, the code will be like this:
Code: Select all
Bundle bundle = new Bundle();
Intent myIntent = new Intent(MainActivity.this, TabsActivity.class);
bundle.putInt("name_as_a_key_to_get_the_value", item_selected);
myIntent.putExtras(bundle);
MainActivity.this.startActivity(myIntent);


Now to fetch this value from TabsActivity, here is how to do this:
Code: Select all
bundle = getIntent().getExtras();
variable_name = bundle.getInt("name_as_a_key_to_get_the_value");


Hope it will help you :)

N.B.: While I'm writing this solution for you I found strider2023 also gave another types of solutions. You can also go for those.
You know there are lots of way out for any problem if you can clearly look into the heart of the problem 8)

Thanks,
mitu
Android is awesome!
User avatar
mitutitu
Senior Developer
Senior Developer
 
Posts: 113
Joined: Tue Jun 09, 2009 7:40 am
Location: St. John's, NL, Canada

Re: Help a newb with creating a variable

Postby igorski88 » Thu Mar 03, 2011 11:13 pm

Thanx to the both of you for the fast response. I ended up using the "if(<ClassName>.Item_Selected == 0)" method.
with help like you guys the world will have better programmers.
igorski88
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 27, 2011 9:03 pm

Re: Help a newb with creating a variable

Postby strider2023 » Sat Mar 05, 2011 6:53 pm

igorski88 wrote:Thanx to the both of you for the fast response. I ended up using the "if(<ClassName>.Item_Selected == 0)" method.
with help like you guys the world will have better programmers.


LOL igorski88 that last line was too much...but thanx m8...we are always ready to help... :)
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests