I have setup Spinner with the layout below, and I wanted to get the value of the item selected, not the displayed text. Where does one pull out the value? Also will this work for pulling out values the layout below? Or do I need some other way to setup (value, displayText) pair? So I guess I need to know how to set it up so it has a value and also how in onItemSelected I would pull the value?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="12dip">
<Spinner
android:id="#+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/some_values"
android:prompt="#array/some_names"/>
</LinearLayout>
You resolve the Spinner instance in your activity class with findViewById and set an OnItemSelectedListener on it. Since you are populating the Spinner with some kind of Adapter, use the items that you constructed the Adapter with and the position of the selected item as reported back in the third parameter of OnItemSelectedListener's onItemSelected method
http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29
I hope it will helpful to you.
Try this Code..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
final Spinner sp2= (Spinner) findViewById(R.id.spinner2);
final Spinner sp3= (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
ArrayAdapter<String> adp2=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,str);
adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adp2);
sp2.setSelection(adp2.getPosition("Report 3"));
ArrayAdapter<CharSequence> adp3=ArrayAdapter.createFromResource(this,
R.array.str2, android.R.layout.simple_spinner_item);
adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adp3);
sp1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
//Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long id) {
// TODO Auto-generated method stub
int selected_item_position = arg2;
String selected_item=sp2.getSelectedItem().toString();
Toast.makeText(getBaseContext(), ""+selected_item_position, Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), selected_item, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp3.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Related
I have created to arrays.One is for all the states(Full Form) in USA and other is the all short form of the states in USA.I have dispayed the states(Full form) in a spinner.Now what i have to do is display the state in short form according to which the user selects in the spinner.How can i achieve it.
You should use mapping i.e in both the arrays the elements should be mapped. Let me give you an example.
String[] fullForm = { Alabama, California, Florida, Illinois};
String[] shortForm = {AL, CA, FL, IL};
ArrayAdapter<String> ad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,fullForm );
spinner.setAdapter(ad);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " You selected " + fullForm[position] + "\n Short form is : " + shortForm[position], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// try this way,hope this will help you...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Spinner
android:id="#+id/spnState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends Activity {
Spinner spnState;
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
spnState = (Spinner) findViewById(R.id.spnState);
String[] stateFullNameArray = new String[]{"statefullname1","statefullname2","statefullname3","statefullname4","statefullname5"};
final String[] stateShortNameArray = new String[]{"stateshortname1","stateshortname2","stateshortname3","stateshortname4","stateshortname5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,stateFullNameArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(adapter);
spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,stateShortNameArray[position],Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
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);
}
}
In my application i have used spinner and edit text.i have created an XML file and set it as background to the spinner which looks like a drop down arrow.When i click the spinner the items selected from spinner should be set in edit text but it displays in edit text as well spinner as the below image. can any one help me with this??
public class newcard extends Activity {
Spinner spinner;
Button btn;
EditText ed,ed1,ed2;
List<String> list;
private String[] countries_list={"01/2014","02/2014","03/2014","04/2014"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewcard);
btn= (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner2);
ed = (EditText) findViewById(R.id.editText3);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
list = new ArrayList<String>();
list.add(" ");
list.add("select");
list.add("01/2014");
list.add("02/2014");
list.add("03/2014");
list.add("04/2014");
ArrayAdapter<String> adp = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg2) {
case 0 :
ed.setText("Select");
break;
case 1 :
ed.setText("01/2014");
break;
case 2 :
ed.setText("02/2014");
break;
case 3 :
ed.setText("04/2014");
break;
default :
ed.setText("Nothing");
break;
}
}
//#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
For setting spinner value to ""
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setText("");
}
how do i add Search Functionality in my android app?
i have a listview in which i want to add search functionality , i have the code but i dont know how do i integrate it properly with my app
here is the code -
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
and here is my java file
package com.Example.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",
"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1", "item 2", "item3", "item 4"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in
// list.
list1.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array));
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true">
<EditText
android:id="#+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:inputType="textVisiblePassword">
</EditText>
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="5dp"
>
</ListView>
</LinearLayout>
Try the below
EditText inputSearch;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
Initialize editText inputSearch in onCreate.
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
Initialize adapter
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
Set the adapter op listview
list1.setAdapter(adapter);
This is where the search happens
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs); // filter based on input
}
Since you are using android's array adapter, you can use its getFilter() method too. So you just have to change minor things in your code.
Instead of setting array adapter like this
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
Create a private variable outside of the methods
private ArrayAdapter adapter;
Then initialize and set it
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);
Now text changed method can call MainActivity.this.adapter.getFilter().filter(cs); without any problem
I'm begginer in android. I'm working on a project. But i get very difficult to do two spinners related to each others. Actually one spinner for the country and another for the city. Instead of the country that is chosen the second spinner will show the cities.
I'v used "OnItemSelectedListener" but the " ArrayAdapter.createFromResource " can't be used inside OnItemSelectedListener.
I've tried a lot of other ways but still none of them working.
Can anybody help me Please???
(P.S. I have read and tried the other posts about this topic but it still doesn't work )
This is the code:
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
int spinnerId = spinner.getSelectedItemPosition();
if (spinnerId==0){
adaptert = ArrayAdapter.createFromResource(
this, R.array.tirana, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
else if (spinnerId==1) {
adaptert = ArrayAdapter.createFromResource(
this, R.array.durres, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
spinnert.setAdapter(adaptert);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
public class AdapterViewImplementation extends Activity implements OnItemSelectedListener{
Spinner sp1; // One Spinner
Spinner sp2; // Another Spinner
ArrayAdapter stateAdapter; // Adapter for state
ArrayAdapter cityAdapter; // Adapter for city
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp1 = (Spinner)findViewById(R.id.Spinner01);
sp2 = (Spinner)findViewById(R.id.Spinner02);
stateAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.state, android.R.layout.simple_spinner_item);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(stateAdapter);
sp1.setOnItemSelectedListener(AdapterViewImplementation.this);
cityAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.city, android.R.layout.simple_spinner_item);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(cityAdapter);
sp2.setOnItemSelectedListener(AdapterViewImplementation.this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg0 == sp1){
sp2.setSelection(arg2);
}else{
sp1.setSelection(arg2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}