I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}
Related
I've a spinner control that I connected it via a dataadapter and I get the data through it .
in the class, I want to get the selected spinner control's id .
How can I do that ?
I've used this code but when I run it , it says there's a problem and it closed .
Toast toast=Toast.makeText(MainActivity.this,sp.getSelectedItemId(),5000);
toast.setGravity(Gravity.CENTER,100, 0);
toast.show();
I tried getSelectedItemId and getSelectedItemPosition but non of them worked .
Here the answer for your question
ArrayAdapter<String> adpt = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, strType);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnType.setAdapter(adpt);
spnType.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int intItemType = spnType.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
This the line getting selected position of the spinner
int intItemType = spnType.getSelectedItemPosition();
Try this
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Please let me know your problem is resolved or not.
I have spinner that have 5 text string. I want to get a string from the spinner, but I only get the first string (i can't get the second, third.....).
i use this syntax(below) but still failed:
Spinner spinner = (Spinner) findViewById(R.id.spinnerItem);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.SpinnerArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String SpinnerText = myspinner.getSelectedItem().toString();
By using onItemSelectedLIstener() method you can get each spinner value into string.
Main.java
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Selected" + selection, 30).show();
}
To get the selected item from the spinner first you need to set the listener for spinner using
spinner.setOnItemSelectedListener(this);
and u need to implement the interface OnItemSelectedListener
and finally override the methods
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
try this code
in onclick listener of spinner use this code to get String of selected item
String s = spinneradapter.getItemAtPosition(Integer.parseInt(position));
hope this help
Use this :
String mySpinner = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
spinner.getItemAtPosition(0).toString()//First string
spinner.getItemAtPosition(1).toString()//second string
spinner.getItemAtPosition(2).toString()//third string
see below code it may help you.
spin_search.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int id,
long arg3) {
Toast.makeText(Sms_logs.this, "you select : " + adapter.getItem(id), 2000).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I have a view object that contains a edittext, an invisible spinner, and a button. I load the view via the LayoutInflater and it shows up and everything works fine. I hit the button, the spinner list shows up and I select the item that I want. My problem is that the setOnItemSelectedListener for the spinner doesn't fire so I can't set the edittext to the selected value.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
final String spnHolder = "spn"+fieldID;
spn.setAdapter(adapter);
ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
bSpn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
}
});
try {
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
curVw.setText(((TextView)arg1).getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
I added a Try...Catch to see if it was just failing past that point, but it is never called.
Instead of using SetOnclickListener please use:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
//implements methods and perform your action. This will works definately
};
No text is being set when I select any item from a spinner. What might be the error?
ArrayAdapter<String> pSelectAdapter = new ArrayAdapter<String>(PCreate.this,android.R.layout.simple_spinner_item, pNames);
pSelectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectP.setAdapter(pSelectAdapter);
selectP.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String nameSelected = pNames.get(arg2);
pID = pMatch.get(nameSelected);
p.setText(nameSelected);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I think here you missed to create the object of View class, Please match this line with your code, and make this change, This may help you..
selectP.setOnItemSelectedListener(new View.OnItemSelectedListener() {
(new View.OnItemSelectedListener() instaed (new OnItemSelectedListener()
I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.