Start New Discussion within our Software Development Community

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

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>

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);

		
	}
	
}

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?

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

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?

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?

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));

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.

This article has been dead for over six months. Start a new discussion instead.