I want to get value from user in launcher activity and use it in default activity for loop.
To pass values from your "LauncherActivity:"
Intent i = new Intent(getApplicationContext(), DefaultActivity.class);
i.putExtra("cycles",5);
startActivity(i);
Then retrieve those values in the "DefaultActivity":
Bundle extras = getIntent().getExtras();
int cycles = 0;
if (extras != null) {
cycles = extras.getInt("cycles");
}
for (int i = 0; i < cycles: i++) {
//do stuff
}
Related
I am creating an app, in which I create some EditTexts dynamically with ID number.
I want to pass the information from the EditTexts, so I tried to create a EditText array with these and then use the .getText().toString() to save them in a String Array, which I want pass to the next activity.
It seems like it won't create the "editArray[]" in second code part correctly.
Thanks in advance.
Here's my code (EnterNames.java) - Creation of EditTexts -> Succesful
protected void NumberOfEditText()
{
View VertLayout = (LinearLayout) findViewById(R.id.VertLayout);
String SpinValue = getIntent().getExtras().getString("SpinValue");
int intSpinValue = Integer.valueOf(SpinValue);
editTextCount = intSpinValue;
EditText[] editTextArray = new EditText[editTextCount];
for (int i = 0; i < editTextCount; i++)
{
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setId(i+1000);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) VertLayout).addView(editTextArray[i]);
}
}
Second code (EnterNames.java) - Passing data to next activity -> Failure.
By testing I think the problem is the for-loops (the editArray returns null)
public void Go(View view)
{
setContentView(R.layout.activity_enter_names);
String NameArray [] = new String [editTextCount];
EditText editArray [] = new EditText [editTextCount];
Intent intent = new Intent(this, RandomGeneration.class);
for (int i = 0; i < editTextCount; i++)
{
editArray[i] = (EditText) findViewById(i+1000);
}
for (int i = 0; i < editTextCount; i++)
{
NameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", NameArray);
intent.putExtras(extras);
startActivity(intent);
}
According to Activity.findViewById() documentation, this method will search for views in your XML, not in views added in Java.
The simple way to do what you want:
class EnterNames extends Activity {
private int editTextCount;
private EditText[] editTextArray;
protected void NumberOfEditText() {
LinearLayout vertLayout = (LinearLayout) findViewById(R.id.VertLayout);
editTextCount = Integer.valueOf(getIntent().getExtras().getString("SpinValue"));
editTextArray = new EditText[editTextCount];
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int i = 0; i < editTextCount; i++) {
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(params);
vertLayout.addView(editTextArray[i]);
}
}
[…]
public void Go(View view)
{
// This will delete views added to your LinearLayout, don't use it!
//setContentView(R.layout.activity_enter_names);
String nameArray[] = new String[editTextCount];
for (int i = 0; i < editTextCount; i++) {
nameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", nameArray);
Intent intent = new Intent(this, RandomGeneration.class);
intent.putExtras(extras);
startActivity(intent);
}
}
Why you are calling setContentView(R.layout.activity_enter_names); in public void Go(View view)? You overide Activities layout in which you previously has added EditText views and all your findById() returns null.
Your code should work if you remove setContentView(R.layout.activity_enter_names); from Go function.
setContentView(R.layout.activity_enter_names); should be called single time in Activities onCreate function.
In my application there are some values in list view and on clicking the list Item am getting values from database and passing it using intent in an array list
ArrayList<String> s = new ArrayList<String>();
s = mDbHelper.fetchDetails(appName);
// s.add(app_formno);
/*
* for(int i =0; i<Data.size();i++){ s.add(Data.get(position));
* System.out.println(Data.get(position)+"*****"+i); }
*/
prefEditor.putBoolean("gotobundle", true);
prefEditor.commit();
startActivity(new Intent(ListViewDetails.this,
ApplicationManagement.class).putExtra("list", s));
I am getting the array list in e bundle which is in Tab widget(TAB 1).
protected void onResume() {
super.onResume();
settings = getSharedPreferences("LandTshare", MODE_PRIVATE);
prefEditor = settings.edit();
boolean bool = settings.getBoolean("gotobundle", false);
if (bool == true) {
Bundle b = getParent().getIntent().getExtras();
// ArrayList<String> bundlevalue = new ArrayList<String>();
bundlevalue = new ArrayList<String>();
if (b != null) {
bundlevalue = b.getStringArrayList("list");
this code prints the respective values in tab 1 but when i go to tab 2 the value in the bundle is empty and my application force closes..
Below is the code in TAB 2
protected void onResume() {
super.onResume();
boolean bool = settings.getBoolean("gotobundle", false);
if(bool == true){
if (Applicant.bundlevalue.size() > 124 == true){
for (int i = 125; i <= 146; i++)
System.out.println(Applicant.bundlevalue.get(i));
if (Applicant.bundlevalue.get(125).equalsIgnoreCase("Yes")) {
rg_existin1.check(R.id.rYes);
} else {
rg_existin1.check(R.id.rNo);
}
what is the issue here?? i cant seem to figure out!
Please help!
I am getting null Values while passing datas through Intent from 1 activity to another activity.
Passing from 1Activity:
int s = position;
String str=adapter.getId(position);
int Type=DeviceType;
Bundle bunch=new Bundle();
bunch.putString("id", str);
bunch.putInt("DeviceType",Type);
bunch.putInt("position", s);
Intent It=new Intent();
It.setClass(PYSActivity.this,GridImages.class);
It.putExtras(bunch);
startActivity(It);
Retriveing here in 2 Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Bundle b = this.getIntent().getExtras();
AppID=b.getString("id");
DeviceType=b.getInt("DeviceType");
Position=b.getInt("position");
list=(GridView)findViewById(R.id.list);
adapter1=new LazyAdapter1(this,mStrings,like,Rate,Id,img);
list.setAdapter(adapter1);
Do something like this:
Activity 1:
int myInt = 5;
String myString = "hi";
...
Intent Intent = new Intent(...);
intent.putExtra("string_key", myString);
intent.putExtra("int_key", myInt);
startActivity(intent);
Activity 2:
int getInt;
String getString;
...
Bundle extras = getIntent().getExtras();
// Read the extras data if it's available.
if (extras != null)
{
getInt = extras.getInt("int_key");
getString = extras.getString("string_key");
}
Why you are creating bundle just use intent.
it.putExtra("id", str);
it.putExtra("DeviceType",Type);
it.putExtra("position", s);
Ok so, I have my app which so far opens a new Activity from the SplashScreen. Once this Activity is open there is a button to add players which opens up a new Activty. In this Activty I have an Array which will be populated by a EditText box.
Once the players have been added the play button will be clicked which will got back to the previous Activity.
My question is when the play button is clicked and you go back to the previous Activty I need the Array to be carried over and populate a String.
CODE UPDATED - No force close now but the array dosent seem to be carrying over and populating strArray
I have this button to carry the Array once players have been added:
Button play = (Button) findViewById(R.id.playnow);
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent( demo.AddRemove.this, demo.play.class);
Bundle extras = new Bundle();
extras.putStringArrayList( "KEY", playerList );
i.putExtras( extras );
startActivity( i );
}});
}
And this to show the Array that was carried over:
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (extras == null) {
strArray = new String[0];
} else {
ArrayList<String> myStrings = extras.getStringArrayList("KEY");
if (myStrings == null) {
strArray = new String[0];
} else {
strArray = (String[]) myStrings.toArray();
}
}
setContentView(R.layout.passw_layout);
initWheel(R.id.passw_1, strArray);
But as I said before When I try to open the first Activty play.java from the SplashScreen it force closes with java.lang.NullPointerException. Which I think is because the extras is empty.
Try this:
String[] strArray;
Intent intent = getIntent();
Bundle extras = intent.getExtras(); //Added this
if (extras == null) {
strArray = new String[0];
} else {
ArrayList<String> myStrings = extras.getStringArrayList("KEY");
if (myStrings == null) {
strArray = new String[0];
} else {
strArray = (String[]) myStrings.toArray();
}
}
Add a simple check for null, and create an empty array:
String[] strArray;
if (extras == null) {
strArray = new String[0];
} else {
ArrayList<String> myStrings = extras.getStringArrayList("KEY");
if (myStrings == null) {
strArray = new String[0];
} else {
strArray = (String[]) myStrings.toArray();
}
}
If you don't like such a deeply nested if statement, you could also create a method to extract the array, and use early return to return the array as soon as you find a null value.
I am a student and new to programming. It might be a simple error, can anybody help me fix it.I have to pass an array list from one activity to another. In this activity i have 5 radio buttons RB1, RB2.... and i want to pass the content of news[] it to another activity called display.
public void onClick(View v) {
String[] news;
news = new String[5];
news[0] = "bbc";
news[1] = "guardian";
news[2] = "yahoo";
news[3] = "sky";
news[4] = "fox news";
final ArrayList<String> arr = new ArrayList<String>();
if (RB1.isChecked() == true)
arr.add(news[0]);
if (RB2.isChecked() == true)
arr.add(news[1]);
if (RB3.isChecked() == true)
arr.add(news[2]);
if (RB4.isChecked() == true)
arr.add(news[3]);
if (RB5.isChecked() == true)
arr.add(news[4]);
if (v == Done)
{
Button done = (Button) findViewById(R.id.DONE);
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent myIntent = new Intent(Read.this, display.class);
myIntent.putExtra("pass",arr);
startActivity(myIntent);
}
});
}
the codes for next activity is as follows
Intent myintent = getIntent();
String[] Array = myintent.getStringArrayExtra("pass");
for (int i = 0; i < Array.length; i++)
Log.v(LOG_TAG, "THE website Is :" +Array[i]);
I am geting a java.lang.NullPointerException in the above two line i.e
for (int i = 0; i < Array.length; i++)
Log.v(LOG_TAG, "THE website Is :" +Array[i]);
can u pls tell me why ?
thanks in advance
First of all, some conventions: Start names of variables lowercase, so array instead of Array. This will save you from some confusion later.
Try it as follows, from this thread:
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
and
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Use Bundle like this:
Bundle bundle = new Bundle();
bundle.putStringArray(key, news);
myIntent.putExtra("pass",bundle);
startActivity(myIntent);