1,105,340 Community Members

Calling Array List from one page to another Java help!!

Member Avatar
mayback230
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have this array in a page and i want to call it into a listview or textview in another page...how do i do that?

public class Screen2 extends Activity {

	public static EditText txt1;
	public static String player;
	public static ArrayList<String> playerList = new ArrayList<String>();

	/** Called when the activity is first created. **/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen2);

		// edittext1 or textview1
		txt1 = (EditText) findViewById(R.id.editText1);
		player = txt1.getText().toString();

		// add more items button
		Button more = (Button) findViewById(R.id.button1);
		more.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				player = txt1.getText().toString();
				if (txt1.getText().toString().length() != 0) {
					playerList.add(player);
					txt1.setText("");
				}
				Toast.makeText(getBaseContext(),
						"Players Added:" + playerList + " ",

						Toast.LENGTH_SHORT).show();

			}

		});

I want to be able to call it in Screen ..help please

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

By "call" I assume you mean you want access to the object so you can call its methods.
One way would be to add a getter method to the class that the arraylist is in so you could call the method and it would return a reference to the arraylist.

You have several variables defined as static. That means there will only be one of them in existence when the code executes. If you create two instances of the Screen2 class, they will share that one instance.

With the way you have the variables coded, you can directly access public static variables in the Screen2 class directly:
Screen2.<staticVariable>

Member Avatar
mayback230
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok....yes by calli mean, access them in another page (screen 3 which has a list view)....
this is my starting code, am i on the right track?

public class Screen3 extends Activity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen3);
		
		Intent intent = getIntent();
		ArrayList<String> playerList = intent.getStringArrayListExtra("arrayListExtra");
		String string = intent.getStringExtra("stringExtra");
		int value = intent.getIntExtra("intExtra", 0);

		
	}
	
}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

What is the posted code supposed to do?
What arraylist is it supposed to be accessing?

Why do you define an arraylist in a method?

Member Avatar
mayback230
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

before i was trying to get the intents i stored in the previous page..now i have this:

public class Screen3 extends Activity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen3);
	
        ListView lv = (ListView) findViewById(R.id.custom_list_view);

        lv.setAdapter(new ArrayAdapter<String>(Screen2.playerList,
                android.R.layout.simple_list_item_1, playerList));

		
	}
	
}

However, the last line has an error saying :

playerList cannot be resolved to a variable
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

You use two different instances of that variable, one on line 11 and one on line 12.
The compiler can't find a definition for the one on line 12.

Where is its definition?

Member Avatar
mayback230
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You use two different instances of that variable, one on line 11 and one on line 12.
The compiler can't find a definition for the one on line 12.

Where is its definition?

I don't seem to be getting it...this is my Screen 2

public class Screen2 extends Activity {

	public static EditText txt1;
	public static String player;
	public static ArrayList<String> playerList = new ArrayList<String>();

	/** Called when the activity is first created. **/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen2);

		// edittext1 or textview1
		txt1 = (EditText) findViewById(R.id.editText1);
		player = txt1.getText().toString();

		// add more items button
		Button more = (Button) findViewById(R.id.button1);
		more.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				player = txt1.getText().toString();
				if (txt1.getText().toString().length() != 0) {
					playerList.add(player);
					txt1.setText("");
				}
				Toast.makeText(getBaseContext(),
						"Players Added:" + playerList + " ",

						Toast.LENGTH_SHORT).show();

			}

		});

		// press Done button to redirect to next activity
		Button done = (Button) findViewById(R.id.button2);
		done.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {

				// start new activity
				Intent myIntent = new Intent(view.getContext(), Screen3.class);
				myIntent.putExtra("arrayListExtra", playerList);
				myIntent.putExtra("stringExtra", player);
				

				startActivity(myIntent);

			}
		});
	}

}

and this is my screen 3:

public class Screen3 extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen3);
	
        ListView lv = (ListView) findViewById(R.id.custom_list_view);

        lv.setAdapter(new ArrayAdapter<String>(Screen2.playerList,
                android.R.layout.simple_list_item_1, playerList));
   

		
	}
	
}

i want to get the array i created and stored through screen two and call it in screen 3 (listview)....
so what am i exactly doing wrong?

Member Avatar
mayback230
Newbie Poster
6 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

got it....i had to switch the

lv.setAdapter(new ArrayAdapter<String>(Screen2.playerList,
android.R.layout.simple_list_item_1, playerList));

to

lv.setAdapter(new ArrayAdapter<String>(Screen3.this,
                android.R.layout.simple_list_item_1, Screen2.playerList));
Member Avatar
Thokozani_1
Newbie Poster
1 post since Aug 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Since your arraylist is marked public and its static, you can just use it like so

Screen2.playerList

Another approach would be to create a class of type PlayerList and implement an interface called Parcelable and pass that object through an intent.

Here is a tutorial on how to use Parcelable

http://www.codexperience.co.za/post/passing-an-object-between-activities-using-an-intent

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

Thokozani_1:
welcome to DaniWeb. sharing your knowledge and solving problems is a good asset, but this problem was solved a year ago, the OP even stated how he solved his problem.

there's no need to revive old threads, especially not those that are already solved.

Member Avatar
jwenting
duckman
8,330 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

might also want to move this to the mobile forum, as it relates to Android development.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: