I want to store the edited spinner value in the database - android

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

Related

How to see text in toast

I'm trying to see text from GridView using a toast when I selected or clicked on GridView's data. I can see position, but don't know what to put to see a text.
Here is a code.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
String [] data = {"a","b", "c","d","e","f", "g","h"};
GridView gdView;
ArrayAdapter<String> adapter;
String result;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
gdView = (GridView) findViewById(R.id.gdView);
gdView.setAdapter(adapter);
gdView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getBaseContext(), position, Toast.LENGTH_SHORT).show();
}
});
}
}
Could someone suggest me, what I need to use or where to look for it?
Thank you
The parameter int position of the method onItemClick() on AdapterView.OnItemClickListener gives you:
The position of the view in the adapter.
See here: https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView, android.view.View, int, long)
You can use this to retrieve the content of this view from your ArrayAdapter with:
adapter.getItem(position)
See here: https://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
This will return the content of your ArrayAdapter at the given position, in your case as a String, which you can store in a variable if you like or pass directly into Toast.makeText().
Turn last string to
Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();

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

add two number using spinner in 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

onListItemClick with Multiple Value/Retrieving Value

**I am trying to retrieve the 2nd value from a Arraylist/ArrayAdapter that I have populated. I am new to Array so please correct me if I am wrong
Q1. I created the Array Favorite. What I think what I created is an Array with two set of value call Detail | Value. example Detail="Yasmin",Value="8". Is this correct?
Q2. I have assign the Favorite Array to the mFavlist listview. During the OnItemClick I can return the label "Yasmin" by the position of the listview. What I would like to do is return the value of "8". What would be the best way to do this?
Please let me know if I am on the right track via the array and adapter**
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class lister extends Activity {
/** Called when the activity is first created. */
TextView txHomeTeam;
protected ListView mFavlist;
protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main);
this.txHomeTeam = (TextView)this.findViewById(R.id.title);
fakeFavs.add(new Favorite("John", "1"));
fakeFavs.add(new Favorite("Yasmin", "8"));
fakeFavs.add(new Favorite("Jack", "10"));
//this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
initListView();
mFavlist.setTextFilterEnabled(true);
mFavlist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item,
//and read the value from <Favorite>.value
//txHomeTeam.setText=????
}
});
}
public void refreshFavListItems() {
mFavlist.setAdapter(new ArrayAdapter<Favorite>(this,
android.R.layout.simple_list_item_1, fakeFavs));
}
public void initListView() {
/* Loads the items to the ListView. */
refreshFavListItems();
}
protected class Favorite {
protected String Detail;
protected String value;
protected Favorite(String Detail, String value) {
this.Detail = Detail;
this.value = value;
}
public String toString() {
return Detail;
}
}
}
Use the position parameter to get to the correct position in your adapter
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
Favorite selectedFav=lister.this.fakeFavs.getItem(position)
}

Categories

Resources