Spinner changing another view

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Spinner changing another view

Postby iaindownie » Wed Dec 09, 2009 2:31 pm

Hi all,

I have a Spinner object/view thing, that when a user selects from I want another view, an <EditText /> to be updated with some simple values. I see in main.xml that there is an onClick function (e.g. android:onClick="handleSpinner"), but even when I create a stub method in my Java class (void that does nothing), my app force closes.

Can someone tell me if it is possible, and how, to update another view using a Spinner? I would have thought this was the most requested feature of Spinners! But I can't get the damn thing to play!

Thanks in advance, hope someone can help as I'm fed up....

Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Top

Postby qlimax » Wed Dec 09, 2009 3:45 pm

java:

Code: Select all
public void handleSpinner(View target) {
        // Do stuff
    }


xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:onClick="handleSpinner"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4



does this work?


btw, I suggest you to use setOnItemClickListener....

bye:)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby iaindownie » Wed Dec 09, 2009 4:56 pm

Hi qlimax,

thanks. Your suggestion for the Java and XML are 'exactly' what I'm doing, but it fails every time:

<Spinner android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_toRightOf="@+id/EditText02"
android:id="@+id/Spinner01" android:layout_below="@+id/midline1"
android:width="100px" android:onClick="mySpinnerHandler" />

and

public void mySpinnerHandler(View v) {
}

I gather this should work, but not for me - frustrated. I've no idea how to implement the "setOnItemClickListener" (I'm a novice to android) - would this be in the Java and/or XML?

Thanks, I appreciate the time/effort
Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Postby qlimax » Wed Dec 09, 2009 6:18 pm

sorry i was wrong, the spinner does not support item click events, you have to implement the setOnItemSelectedListener


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <Spinner
  2. android:id="@+id/Spinner01"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. ....
  6. >
  7. </Spinner>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 ArrayList<String> values=new ArrayList<String>();
  2.                 values.add("first value");
  3.                 values.add("second value");
  4.                 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, values);
  5.                 Spinner spinner=(Spinner) findViewById(R.id.Spinner01);
  6.                 spinner.setAdapter(adapter);
  7.                 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  8.  
  9.                         @Override
  10.                         public void onItemSelected(AdapterView<?> arg0, View arg1,
  11.                                         int selectedPosition, long arg3) {
  12.                                
  13.                                 if(selectedPosition==1){
  14.                                         aTextView.setText("you selected the second value");
  15.                                 }
  16.                                
  17.                         }
  18.  
  19.                         @Override
  20.                         public void onNothingSelected(AdapterView<?> arg0) {
  21.                                 // TODO Auto-generated method stub
  22.                                
  23.                         }
  24.                 });
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



let me know...
bye ;D
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby iaindownie » Wed Dec 09, 2009 6:41 pm

BRILLIANT! Thanks very much.

Now all I need to do is establish the element at position 0 to not auto populate the EditText until the user selects. Any ideas on this last bit? Once I've this sorted I'm almost done!! I can do it cleverly by creating a Clear option as the first real element, that will call setText(""); but that seems a bit weird.

Iain
User avatar
iaindownie
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon Dec 29, 2008 4:08 pm
Location: Thetford, Norfolk, UK

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests