I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]
Related
I am trying to display image in image view, based on spinner selection. If anyone know how to solve this please help.
public class activity_tables extends AppCompatActivity {
Spinner s1;
String[] tbls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tables);
s1 = (Spinner)findViewById(R.id.spinner);
tbls = getResources().getStringArray(R.array.tables);
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tbls);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
}
Set onItemSelectedListener for your spinner like this
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(position==0)
{
qImageView.setImageResource(R.drawable.whatever);}
else if(position ==1)
{
qImageView.setImageResource(R.drawable.whatever);}
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I want to set text from selected item from spinner to textview, But when i tried to do application craches. Can any one help me .
public class MainActivity extends AppCompatActivity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
String result = sp.getSelectedItem().toString();
txt.setText(result);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
}
sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
This may help Frnd.
spinner has no "getSelectedItem". https://developer.android.com/reference/android/widget/Spinner.html https://developer.android.com/guide/topics/ui/controls/spinner.html
and here's how you get the text-value of the selected row: https://stackoverflow.com/a/15545909/3022387
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Implement spinner.setOnItemClickListener and then find text inside that method like this.
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
You can't get selected Item before setting adapter.Try this below code
public class MainActivity extends AppCompatActivity {
final TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable
I create 4 spinner views in my application, when I change the selected item on one of them I want to know which one of spinners is selected and changed. I use this code :
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
How can I set a unique tag for each spinner and return this tag on ItemSelectedListner?
thanks
This does it:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setTag(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getTag().equals("0")){
//your code
}
else if (parent.getTag().equals("1")){
//your code
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
Use parent.getId() in your onItemSelected() to get the Id of the spinner whose item is selected. The working code:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setId(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(parent.getId()){
case "0": //your code
case "1": //your code
...
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
It looks pretty straight forward but it's not working for me. I am trying to get item text from a Spinner when Item is selected.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
It's returning "android.database.sqlite.Sq;LiteCursor#40e06dc0"
What am I doing wrong ?
well you are getting the string representation of the object which is memory address.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp.getSelectedItem().toString() //returns "android.database.sqlite.Sq;LiteCursor#40e06dc0"
selectedItemView.getText() or sp.getSelectedItem().getText() should do the trick.