Changing TextView value with change in selectedItem on Spinner - android

I am totally at my wits end with trying to change the value of a TextView based on what is selected in the adjoining Spinner.
public class SpinnerSelectItemListener implements OnItemSelectedListener {
private Context context;
public SpinnerSelectItemListener(Context c){
this.context = c;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
View view = null;
TextView textView = null;
LayoutInflater inflater = LayoutInflater.from(context);
parent.getItemAtPosition(position);
view = new View(context);
view = inflater.inflate(R.layout.common_app_header, null);
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText("John");
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
When I run this is debug mode everything is happening as expected but when all is done, the value of the textView doesn't change on the emulator even when the debugger is showing the new value.
There is definitely something really silly that I am missing. Please help.
EDIT: The situation is something like I selected the id number of an employee from the spinner and depending on the selection, the TextView displaying the employee's name changes. The TextView I want to modify is outside the spinner.
EDIT2: This runs fine when I define the listener inline i.e. I write something like
modelspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView textView = null;
textView = (TextView)findViewById(R.id.customer_segment_value);
textView.setText("Commercial");
textView = (TextView)findViewById(R.id.TIV_value);
textView.setText(R.string.app1_name);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Can someone explain what is wrong with the code that I had written earlier.

See here
http://developer.android.com/guide/topics/ui/controls/spinner.html
Modify this method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
to
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String value = (String) parent.getItemAtPosition(pos)
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText(value );
}
but I recommend you to move
textView = (TextView)view.findViewById(R.id.customer_name_value);
to the method onCreate of your Activity

I had a similar issue. I fixed it by getting the textview before I got into the onItemSelected. In my case, the spinner was part of a dialog. Inside the onCreateDialog, that is where I fetched the textview.
protected Dialog onCreateDialog(int id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
switch (id) {
case DIALOG_ADD:
builder.setTitle("Create New Action");
final View textEntryView = getLayoutInflater().inflate(
R.layout.addactionrow, null);
builder.setView(textEntryView);
workingAmount = (TextView) textEntryView
.findViewById(R.id.WorkingActionamount);
Then inside the OnItemSelected I just used the textview and things started working as expected.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view,
int pos, long id) {
spinnerSelectedAssetID = id;
//Get the amount currently held here.....
long x = pfdata.getActionCurrentTotalForAssetByID(spinnerSelectedAssetID);
Log.d("X+", "X="+x);
workingAmount.setText(Long.toString(x));
workingAmount.setVisibility(View.VISIBLE);

Try This Code:
public class MainActivity extends Activity {
String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
"WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};
Spinner spinner1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.text1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}
OnItemSelectedListener onItemSelectedListener1 =
new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(val1[position]);
textView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};
}
XML CODE
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Try this code.And main key in this is " bResult.setText( spinner1csr.getString( spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID1) ) );"
public class MainActivity extends AppCompatActivity {
Spinner s1, s2, s3;
TextView tex, tex1, bResult;
Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr, search;
SimpleCursorAdapter sca, sca2, sca3, sca4, sca6;
long spinner1_selected = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
s3 = (Spinner) findViewById(R.id.spinner5);
final TextView bResult = (TextView)
findViewById(R.id.barcodeResult);
dbhndlr = new DatabaseHandler(this);
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsAsCursor();
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, spinner1csr,
new String[] {
DatabaseHandler.KEY_ID
},
new int[] {
android.R.id.text1
},
0
);
s1.setAdapter(sca);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
// bResult.setText(s1.getSelectedItem().toString());
spinner1_selected = id;
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {}
});
spinner4csr = dbhndlr.getByRowid(spinner1_selected);
sca4 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner4csr,
new String[] {
DatabaseHandler.KEY_ID1
},
new int[] {
android.R.id.text1
},
0
);

Related

Open Text plain when spinner option get selected

i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

How to set selected country isd code in spinner

I don't know how to set the selected country isd code in
spinner.Below I have posted the code what I have been tried so far:
MainActivity.java:
public class MainActivity extends Activity implements OnItemSelectedListener {
ArrayList<String> arrCode;
ArrayList<String> arrCountry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
arrCode = new ArrayList<>();
arrCountry = new ArrayList<>();
arrCountry.add("US");
arrCountry.add("KZ");
arrCountry.add("EG");
arrCountry.add("ZA");
arrCountry.add("GR");
arrCode.add("1");
arrCode.add("7");
arrCode.add("20");
arrCode.add("27");
arrCode.add("30");
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrCountry);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_mobile_code.setAdapter(adapter_state);
sp_mobile_code.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
int spinnerValue1 = sp_mobile_code.getSelectedItemPosition();
String data = arrCode.get(spinnerValue1);
Log.e("data", "" + data);
sp_mobile_code.setPrompt(data);
/* sp_mobile_code.setSelection(position);*/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
My issue is : I can't set the isd code in same spinner.If I have to
set isd code in textview means it will be easy.But I have to set it
in the spinner
Edit: spinner list showing correctly the country list.on clicking the country list, I have to show the isd code
Use Custom ArrayAdapter.
private static class CustomSpinnerAdapter extends ArrayAdapter<String>
{
List<String> arrCodes;
public CustomSpinnerAdapter(Context context, int resource, List<String> items, List<String> arrCodes)
{
super(context, resource, items);
this.arrCodes = new ArrayList<>();
this.arrCodes = arrCodes;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView view = (TextView) super.getView(position, convertView, parent);
view.setText(arrCodes.get(position));
return view;
}
}
Check the position in the getView() and set text of your desire. Use this adapter for the Spinner

android spinner if some item selected, other spinner gone

spinner a have 2 item : poison and non_poison
spinner b have 2 item : dart frog and viper
spinner c have 2 item : chicken and eggs
long story short, i want to make
"IF" selected item on spinner a = poison
"THEN" spinner b = visible, spinner c = gone
heres my code, tried using setOnItemSelectedListener, no error shown, but its not working the way it supposed to be, am i missing something in here ? please help me....
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1x = spinner1.getSelectedItem().toString();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
return mRelative;
}
}
You forgot to retrieve the currently selected item of your spinner & save it to your spinner1x string, & to implement the onNothingSelected method:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
spinner1x = mRelative.getItemAtPosition(position).toString();
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Android Spinners
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// Use spinner item position for this.
if (position == 0){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
I am just using your code with corrections..
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
spinner1x = spinner1.getSelectedItem().toString();
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}else {
spinner2.setVisibility(View.GONE);
spinner3.setVisibility(View.VISIBLE);
}
}
});
return mRelative;
}
}

Using Spinner And List Views with setOnItemSelectedListener

I'm supposed to make a task , I have a spinner connected with a String array x , this array contains three values , so I want when clicking any choice of the spinner a specific list view will will give a specific three values and , this is my code :
public class Four extends ActionBarActivity {
String x [] = {"Jordan","Saudi Arabia", "Syria"};
String Jordan[] = {"Amman","Aqaba","Sarqa"};
String Saudi[] = {"Riyadh","Jeddah","Khobar"};
String Syria[] = {"Hems","Halab","Demashk"};
Spinner sp1 ;
ListView lv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
lv1 = (ListView)findViewById(R.id.listView1);
// Jordan List View
ArrayAdapter<String> jor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Jordan);
lv1.setAdapter(jor);
// Saudi Arabia List View
ArrayAdapter<String> saud = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Saudi);
lv1.setAdapter(saud);
// Syria List View
ArrayAdapter<String> syr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Syria);
lv1.setAdapter(syr);
sp1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , x);
sp1.setAdapter(a);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
The thir parameter of onItemSelected() (int arg2 in your example, but I would suggest you rename them) is the position you selected in the Spinner. So you could implement that method like this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position == 0)
lv1.setAdapter(jor);
else if (position == 1)
lv1.setAdapter(saud);
else if (position == 1);
lv1.setAdapter(syr);
}
Remember that all the variables involved (lv1, jor, saud, syr) must be defined as final to be used inside the anonymous class, e.g.
final ArrayAdapter<String> jor = ...
final ArrayAdapter<String> saud = ...

setting itemListener of variable number of spinners

int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources