The problem in EditText is, it is not returning the input value.I am using that input value to check condition.
I am giving the correct input so that i checks the if condition and go to next activity but
it always goes to else condition.
Please check the code below, i had commented the problem.
Here is the snippet
public class BuildWord extends Activity
{
String word = "word";
String finished = "Word Built";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);
EditText get = (EditText)findViewById(R.id.dataToSend);
String getdata = get.getText().toString(); //Here i am getting Data from EditText
displayIntentData();
if (getdata.equals("word"))
//Here i am checking with "word" but it goes to else condition.I am typing "word" only
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", word);
startActivity(intent);
}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}
private void displayIntentData()
{
Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData1);
Bundle extras=intent.getExtras();
if(extras!=null)
{
tv.setText("Data received: "+extras.getString("key"));
}
else
{
tv.setText("No extradata received");
}
}
}
After displayIntentData(); declare String getdata = get.getText().toString();
you are getting the value in the EditText field ("get") immediately after inflating the view. you couldn't have time to enter any text yet, so it is empty. you then check if "getData" contains "word" (it doesn't!) therefore you're hitting the ELSE.
We will get the text when any click event is happened ....
String getdata = get.getText().toString(); //Here i am getting Data from EditText
above code always return empty string ...
try put the code in on click event and process so that you can get the string and compare it easily
You are getting input data right after when the view is being created.
Instead try to take input data inside your click listener
Related
I have 2 classes AddCategory and SaveData. AddCategory contains a edit text and a button ADD. Second class contains a spinner and some edit texts. I want to add Some data from 1st class to second class that is Edit text input to Spinner. How can I parse value from first class to second class? Please explain with example..
ADD CATEGORY CLASS
btnAdd.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
call();
}
});
}
public void call()
{
cat=edtCategory.getText().toString();
list.add(cat); <<<<<*** I want to read and insert this arraylist into spinner in SAVE DATA class ****>>>>>>
}
}
SAVE DATA CLASS
Spinner category;
Button btnSave;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.income_add);
Intent i=getIntent();
category=(Spinner)findViewById(R.id.spinner01);
btnSave=(Button)findViewById(R.id.button01);
//* I put a Log here to read arraylist from ADD CATEGORY class, getting null value here. I need to read the arraylist and insert into spinner* //
}
Try this:
String edittextvalue=yourEdittext.getText().toString();
Intent i = new Intent(AddCategory.this, SaveData.class);
i.putStringArrayListExtra("list", list);
startActivity(i);
In SaveData class:
Intent intent=getIntent();
ArrayList<String> list=new ArrayList<String>();
list=intent.getStringArrayListExtra("list");
ArrayAdapter<String> ad =new ArrayAdapter<String>(this,layoutId, list);
spinner.setAdapter(ad);
You can pass values from AddCategory Class to SaveData Class with the help of
Shared preferences
Use this to "put" the file (In AddCategory class)...
Intent i = new Intent(AddCategory.this, SaveData.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Then , to retrieve the value try something like:(In SaveData class)
String newString;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
I'm currently trying to guess how do I save a data from a previous Activity.
An example is:
At the startPage.class, I have a few options to choose from(Animation Mode, Image Mode, Text Mode) so if I choose for example Text Mode so the it'll be the RadioButton3 and when I press next it goes to the another Activity. So lets say in that new Activity it has this Intent command. How do I retain the data from the previous activity when I press the backSelection3?
Meaning, when I press the back, I want the RadioButton3 to be the selection still instead of it resetting to the default choice.
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent backSelection3 = new Intent(imagemode64by64.this, startPage.class);
startActivity(backSelection3);
}
});
You can use onSaveInstanceState
void onSaveInstanceState(Bundle out) {
String val = ...
out.putString("MYVALUE", val);
super.onSaveInstanceState(val);
}
Then
void onCreate(Bundle savedState) {
if(savedState != null) {
String val = savedState.getString("MYVALUE");
}
}
Or do you mean how to put data for another activity? Then you can do
Intent i = new Intnet(this, OtherActivity.class);
String val = ...
i.putExtra("MYVALUE", val);
startActivity(i);
Then in the other activity
void onCreate(Bundle savedState) {
...
Intent i = getIntent();
String val = i.getStringExtra("MYVALUE");
}
Here is an age old example of passing data between classes:
class A{
static int num = 0;
public void setNum(int number){
num = number
}
}
class B{
public static void main(){
A obja = new A();
obja.setNum(3);
}
}
As soon as you do the operation in class B you can use the num variable in class A.
I have two class Profile.class and Details.class,
In profile class i have used a spinner with values like (ATM,Banking,Personal,Others etc)
and a button (OK).
on clicking ok button it will go to next activity that is details activity where i will be taking some details like-name,description etc.
after filling the details i have given a button (save).
on clicking button save i will be saving the name and description in database but i want to save the profile name also along with details. i am unable to transfer selected spinner text from Profile.class to Details.class
how to transfer?
create.class code
public class Create extends Activity {
public ArrayList<String> array_spinner;
Button button4;
String spinnertext;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
array_spinner=new ArrayList<String>();
array_spinner.add("ATM");
array_spinner.add("Bank");
array_spinner.add("Mail");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, array_spinner);
adapter.setNotifyOnChange(true);
spinner.setAdapter(adapter);
spinner.setLongClickable(true);
spinner.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}}
);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent4 = new Intent(view.getContext(), Details.class);
startActivityForResult(myIntent4, 0);
myIntent4 .putExtra("key", array_spinner.getSelectedItem().toString());
startActivity(myIntent4);
}
});
}}
details.class code
public class Details extends Activity {
EditText editText4,editText5,editText6;
Button button8,button9,button10;
TextView textView7;
String et4,et5,et6;
//SQLite Database db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
String spinnervalue = getIntent().getExtras().getString("Key");
please kindly explain me what is this "key"?
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
example
this is what you have to write in your first activity
Intent i = new Intent(getApplicationContext(), Product.class);
i.putExtra("productname", ori);
i.putExtra("productcost", position);
i.startActivityForResult(i,0);
then in your next activity you need to have this code
String productname,productcost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product);
tv1= (TextView)findViewById(R.id.tv1);
tv2= (TextView)findViewById(R.id.tv2);
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
position = extras.getString("position"); // get the value based on the key
tv1.setText(productname);//use where ever you want
productname = extras.getString("productname"); // get the value based on the key
tv2.setText(productname);
}
First of all take a spinner and provide value to them what you want and then the selected spinner value change it to string value and this string variable will be used in OK button to pass value through use of Intent or Shared preference to take this value to another activity and through there you can use it in database to display this value.
If you want to send data to another activity, you can do it using intent.
Bundle bund = new Bundle();
bund.putString("myKey",name);
Intent intent = new Intent(Profile.this, Detail.class);
intent.putExtras(bund);
startActivity(intent);
Now in Detail class, receive this data in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
.......
String nameReceived = getIntent().getExtras().getString("myKey");
}
I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here
i have 10 edittext values in activitya,i want to add them and show the result in newactivity.
my code
public class activitya extends Activity implements onclicklistener
{
private EditText editext1;
private EditText editext2;
private EditText editext3;
private EditText editext4;
private EditText editext5;
private Button calculate;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitya);
calculate =(Button)findviewbyid(R.id.button1);
calculate.setonclicklistener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
onadd();
break;
default:
break;
}
}
public void onadd()
{
Bundle bundle = new Bundle();
String value1 = editext1.getText().toString();
String value2 =editext2.getText().toString();
bundle.putString("sendvalue1",value1));
bundle.putString("sendvalue2",value2));
try{
Double Total =Double.parseDouble(value1)+Double.parseDouble(value2);}
catch(NumberFormatException e)
{
e.printstacktrace();
}
bundle.putString("totalvalue",String.valueof(Total));
Intent a = new intent(this,newactivity.class);
a.putExtras(bundle);
startactivity(a);
}
}
in newactivity :
public class newactivity extends activity{
private TextView total;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
total =(TextView)findviewbyid(R.id.text1);
onview();
}
public void onview()
{
Bundle bundle = this.getIntent().getExtras();
String param1 = bundle.getString("totalvalue");
total.setText(param1);
}
it shows the addition correctly only if i enter all the 10 values, strange
if i dont enter even one value it shows zero,
couldnt understand where the problem is
You have a try-catch and you do nothing in the catch. This is usually bad practice since you do not know when anything happens in your code.
In this case, Double.parseDouble is throwing an exception as you are trying to parse empty Strings. "" cannot be parsed to a double. "0" can.
You need to make sure that every edit text contains a valid value which can parse to a double or, test each value before trying to parse it.
To check all EditText length before you add all the values.
Like,
if(EditText1.getText.length()==0){
Toast.makeText(YourActivity.this,"All fields are mandatory ",
Toast.LENGTH_LONG).show();
}else if(EditText2.getText.length()==0){
} else if(){
..........
}else{
}
You will avoid NullPointerException
In my projects I kept different images for categories. When I click on each category image I am passing its category id to other page statically and setting the drawable image of that category in the new page.
My Code:
Firstpage.java
public static String categoryid;
category1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="0";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
category2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="1";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
category3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
categoryid="2";
Intent myIntent = new Intent(view.getContext(),Nextpage.class);
startActivityForResult(myIntent, 0);
}
});
Nextpage.java
public static String catid = Firstpage.categoryid;
ImageView categorytype=(ImageView)findViewById(R.id.imageView1);
if(catid=="0")
{
categorytype.setBackgroundResource(R.drawable.image1);
}
else if(catid=="1")
{
categorytype.setBackgroundResource(R.drawable.image2);
}
else if(catid=="2")
{
categorytype.setBackgroundResource(R.drawable.image3);
}
First time when I am clicking on the category image it is passing the category id to the next page and that particular image is setting in the nextpage. After that I clicked the back button(android back button) and went to Firstpage.java and again clicked on other image. But this time also the same image stored. The category id didnt changed. The category id is not refreshing...How to refresh the category id? Any suggestion will be thankful.....
You are comparing two strings by == operator, instead you should compare by equals method, try following:
if(catid.equals("0"))
{
categorytype.setBackgroundResource(R.drawable.image1);
}
else if(catid.equals("1"))
{
categorytype.setBackgroundResource(R.drawable.image2);
}
else if(catid.equals(2"))
{
categorytype.setBackgroundResource(R.drawable.image3);
}
Don't use static variables for this. Pass the category ID to your Nextpage activity through the intent. In Firstpage.java:
public static final String CATEGORY_KEY = "category";
category1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(this,Nextpage.class);
myIntent.putExtra(CATEGORY_KEY, "0");
startActivityForResult(myIntent, 0);
}
});
Then retrieve it in the onCreate(Bundle) method of Nextpage.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String categoryid = intent.getStringExtra(Firstpage.CATEGORY_KEY);
. . .
}
The reason your method doesn't work is that Nextpage.catid is initialized when the class is loaded, but the assignment statement is not executed again unless the class is unloaded and needs to be reloaded.
I think problem is here you are comparing String values using this if(catid=="0") Which you should be compare it using if(catid.trim().equals("0"))
Update this change in your code and check it.