Anurag Joshi
Anurag Joshi

Reputation: 235

Changing TextView value with change in selectedItem on Spinner

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.

Upvotes: 1

Views: 5984

Answers (4)

Leace
Leace

Reputation: 262

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

      );

Upvotes: 0

Nandkishor mewara
Nandkishor mewara

Reputation: 2562

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>

Upvotes: 0

Clavijo
Clavijo

Reputation: 64

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

Upvotes: 0

avesha
avesha

Reputation: 136

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

Upvotes: 1

Related Questions