add two number using spinner in android - android

i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}

This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to

Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item

Related

Problems with retaining user inputted form data when switching between activities

If anyone could help me with the following problem I would be eternally grateful.
My android app is a questionnaire to carry out property surveys. Each activity relates to an element of the property i.e. kitchen, bathroom, central heating etc. There will be circa 50 questions when the app is complete. Each activity has three Spinners and two Edit Texts with which the user must input data relating to the age and condition of the relevant element of the property before moving onto the next activity.
My problem is as follows:
Question 1 relates to the kitchen. Once all the relevant data is inputted I use an intent (via a 'next page' button) to start the next activity which relates to the bathroom. However, if I realise I made an error with my data input on the kitchen activity and go back via an intent from the bathroom activity (in the same way I got to the bathroom activity from the kitchen activity) the data I previously inputted is no longer there.
How do I retain this data? It is essential that my app users can flick backwards and forwards between the survey questions and view the data they have previously inputted. Once all 50 questions have been answered the data will be saved to a database and the next property can then be surveyed.
I have trawled the internet and various books for the answer to this but I am encountering conflicting information. Some say use on Pause, others say on Stop, others say on Saved Instance State. I'm confused?? I've been stuck on this for three days now so any help is very much appreciated.
Kitchen Activity below followed by Bathroom Activity..........
package com.example.basicview6;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Kitchen extends Activity {
// defining the variables that will be displayed on the page
String[] age, renewal, main;
Spinner s1, s2, sMain;
ToggleButton repairs;
EditText repDesc, repCost, quantity;
Button back, next;
TextView life, qty, unit;
// creating the layout from the main xml file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kitchen);
// getting the string array values from the 'strings xml' resources file
// and applying them to the relevant variable
age = getResources().getStringArray(R.array.age_array);
renewal = getResources().getStringArray(R.array.renewal_array);
main = getResources().getStringArray(R.array.kitchen_array);
// getting the Spinner widget from the main xml and applying it to the
// 's1' variable
s1 = (Spinner) findViewById(R.id.spAge);
s2 = (Spinner) findViewById(R.id.spRenewal);
sMain = (Spinner) findViewById(R.id.spKitchen);
repairs = (ToggleButton) findViewById(R.id.tbRepairs);
repDesc = (EditText) findViewById(R.id.etRepDesc);
repCost = (EditText) findViewById(R.id.etRepCost);
quantity = (EditText) findViewById(R.id.etQuantity);
back = (Button) findViewById(R.id.bBack);
next = (Button) findViewById(R.id.bNext);
life = (TextView) findViewById(R.id.tvLife);
qty = (TextView) findViewById(R.id.tvQuantity);
unit = (TextView) findViewById(R.id.tvUnitM);
life.setVisibility(View.INVISIBLE);
quantity.setVisibility(View.INVISIBLE);
qty.setVisibility(View.INVISIBLE);
unit.setVisibility(View.INVISIBLE);
/*
* creating a new 'string type' ArrayAdapter and telling it to display
* the values of the relevant variable as a simple spinner item
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, age);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, renewal);
ArrayAdapter<String> adapterM = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, main);
// priming the s1 Spinner variable for an array item to be selected -
// standby mode
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sMain.setAdapter(adapterM);
sMain.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// setting up the OnClickListerner for the repairs toggle button
repairs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (repairs.isChecked()) {
repDesc.setVisibility(View.VISIBLE);
repCost.setVisibility(View.VISIBLE);
} else {
repDesc.setVisibility(View.INVISIBLE);
repCost.setVisibility(View.INVISIBLE);
}
}
});
// setting up the OnClickListener for the Next button
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openBathroom = new Intent(
"com.example.basicview6.BATHROOM");
startActivity(openBathroom);
}
});
}
}
BATHROOM ACTIVITY ...................
package com.example.basicview6;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Bathroom extends Activity {
// defining the variables that will be displayed on the page
String[] age, renewal, main;
Spinner s1, s2, sMain;
ToggleButton repairs;
EditText repDesc, repCost, quantity;
Button back, next;
TextView life, qty, unit;
// creating the layout from the main xml file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bathroom);
// getting the string array values from the 'strings xml' resources file
// and applying them to the relevant variable
age = getResources().getStringArray(R.array.age_array);
renewal = getResources().getStringArray(R.array.renewal_array);
main = getResources().getStringArray(R.array.bathroom_array);
// getting the Spinner widget from the main xml and applying it to the
// 's1' variable
s1 = (Spinner) findViewById(R.id.spAge);
s2 = (Spinner) findViewById(R.id.spRenewal);
sMain = (Spinner) findViewById(R.id.spBathroom);
repairs = (ToggleButton) findViewById(R.id.tbRepairs);
repDesc = (EditText) findViewById(R.id.etRepDesc);
repCost = (EditText) findViewById(R.id.etRepCost);
quantity = (EditText) findViewById(R.id.etQuantity);
back = (Button) findViewById(R.id.bBack);
next = (Button) findViewById(R.id.bNext);
life = (TextView) findViewById(R.id.tvLife);
qty = (TextView) findViewById(R.id.tvQuantity);
unit = (TextView) findViewById(R.id.tvUnitM);
life.setVisibility(View.INVISIBLE);
quantity.setVisibility(View.INVISIBLE);
qty.setVisibility(View.INVISIBLE);
unit.setVisibility(View.INVISIBLE);
/*
* creating a new 'string type' ArrayAdapter and telling it to display
* the values of the relevant variable as a simple spinner item
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, age);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, renewal);
ArrayAdapter<String> adapterM = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, main);
// priming the s1 Spinner variable for an array item to be selected -
// standby mode
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sMain.setAdapter(adapterM);
sMain.setOnItemSelectedListener(new OnItemSelectedListener() {
// telling the program what to do when an item is selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// setting up the OnClickListerner for the repairs toggle button
repairs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (repairs.isChecked()) {
repDesc.setVisibility(View.VISIBLE);
repCost.setVisibility(View.VISIBLE);
} else {
repDesc.setVisibility(View.INVISIBLE);
repCost.setVisibility(View.INVISIBLE);
}
}
});
// setting up the OnClickListener for the Back button
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openKitchen = new Intent(
"com.example.basicview6.KITCHEN");
startActivity(openKitchen);
}
});
}
}
Well you can use a number of different methods which is why you keep seeing conflicting answers. However there are a few that are preferred.
For screen rotation, meaning if the device is flipped horizontally or vertically you use onSaveInstanceState.
There is another method I consider cheating and many people will tell you to stay away from as it can cause many errors down the line. However it is the easiest way possible. Simply go into your manifest and place this line
android:configChanges="orientation|keyboardHidden|screenSize
If you are trying to retain information so that when you return to an Activity it is still there you should use onPause(). It appears as though you are only transferring simple data like String, int, etc. This can be done using SharedPreferences inside your onPause(). The reason is onPause() is always (99% of the time) called before the Activity is killed, meaning you will always retain your information.
For some video references to show you how to do these things go here The New Boston
If you would rather read here are some links
SharedPreferences
onPause or onPause
onSaveInstanceState
If you need anything else just ask.
When switching between the Activities you can put extras to the Intent: putExtra() like:
intent.putExtra(Intent.EXTRA_TEXT, "my saved String");
And get them in the called activity by:
String gottenString = intent.getExtras().getString(Intent.EXTRA_TEXT);
You can also save your data in a (temporary) file: Saving Files

How to make fragment transaction when clicked on a listitem?

I have an activity where it contains an ActionBar (with four tabs) a fragment respectively assigned to each of these tabs. In these fragments I've assigned some ListAdapters filled with string values, clickable that furthermore I want to operate. When clicking on an item I want that app to send from that fragment to another. I know that I have to use FragmentManager() and FragmentTransaction() but since I'm new to Android dev I demand of any kind of help, help that is appreciated.
Here's the snippet code of one of the tabs(UserFragment.java):
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
The Profile.java activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}
You need to define OnItemClickListener for your ListFragment to handle item click events. For example:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// start your new activity here
}
});
I found what was wrong. After a long search I learnt that if the onCreateView() method is static than it's all good to set listener/s but in this case while we fill an array-adapter of string than it's a no-go since first of first it has to created its View therefore doesn't let the app to make any further listeners. In order to make that available, the onActivityCreated(Bundle) should be initiated/created between onCreateView() and onStart() methods and insert the rest of the code.
Here's the solution to link a ListFragment to another FragmentActivity class:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
}

Spinner listener not working

my app crashes with this code.. it doesnt even start up.. any ideas guys thanks
my app crashes with this code.. it doesnt even start up.. any ideas guys thanks
package com.about.bysk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;
public abstract class AboutActivity extends Activity implements
OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spin);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(null, "a", 5);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
this makes my app crash. please help..
You have to set Listener for your spinner and your class must implement OnItemSelectedListener
public class YourClass extends Activity implements OnItemSelectedListener { ... }
Then you must set Listener for your spinner:
spinner.setOnItemSelectedListener(this);
Or you can use it like anonymous class
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { ... }
#Override
public void onNothingSelected(AdapterView<?> parentView) { ... }
});
Note: If you want to show Toast, you must call show() method.
You forgot to setlistner ... Also you din call show method with toast !!
you have not set the lisnter to spinner
as per you code do as below ...
1-public class AboutActivity extends Activity implement OnItemSelectedListener{
2- spinner.setOnItemSelectedListener(this);
3- Toast.makeText(AboutActivity.this,"RootBox",Toast.LENGTH_LONG).show();
you can't pass null as context to Toast
Toast.makeText(AboutActivity.this, "a", Toast.LENGTH_LONG).show();

How to use the seletedItem from the AutoCompleteTextView dropdown list?

Friends, I am using AutoCompleteTextView. The Suggestion :
String[] recipes={ "Fish", "Chicken", "Mutton"};
How do I do this:
When I select one of the item from the dropdown list, it will go to another event?
For example, I type Fi, it will come out Fish from the dropdown list and then I select Fish, it will go to another Activity.
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.view.View;
import android.content.Intent;
public class AutoCompleteTextActivity extends Activity {
String[] recipes ={
"Nasi Lemak With Ikan Bilis",
"Steamed Cod Fish"
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, recipes);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes);
textView.setThreshold(3);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View view, int index, long id){
Intent i=new Intent(this,Activity2.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
});
}
}
You can use a class that implements TextWatcher and override following methods:
#Override
public void afterTextChanged(final Editable editable) {
// check if entered text is "fish" and if yes then start the new activity.
}
#Override
public void beforeTextChanged(final CharSequence string,
final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence string, final int start,
final int before, final int count) {
}
}
receipesBelow Snippet will help you.
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> av, View view,
int index, long id)
{
//index will give you item which you selected
Now start another activity here
Intent i=new Intent(context,SecondActivity.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
})
SecondActivity.java
You can retrieve that value is onCreate like below
String selectedItem=getIntent().getStringExtra("item");
You should have used setOnItemClickListener instead of setOnItemSelectedListener.

I want to store the edited spinner value in the database

my coding is working well .But I want to store the edited spinner value in the database how it be done.here is my code
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class tooo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner localSpinner = (Spinner)findViewById(R.id.color_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color_array, R.layout.my_normal_spinner_item_style);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(adapter);
}
}
my simple_spinner_dropdown_item.xml
Add follwoing after setting the adapter in your code
localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = localSpinner.getSelectedItem().toString();
//The above text variable has the selected value of spinner
}
public void onNothingSelected(AdapterView parentView)
{
}
});
Why didnt you edited question in your last post rather than creating a new post

Categories

Resources