Listactivity : is there a onFocus method ?

Put your problem here if it does not fit any of the other categories.

Listactivity : is there a onFocus method ?

Postby Lex » Sat Dec 15, 2007 1:59 am

Hi everyone,
I'm using a ListActivity,
I'd like to display a showAlert everytime an item of the list is focused ( and I dont mean CLICKED ! ).
There is a onListItemClick method, but it's used when the item is clicked ! How would u do to make an action when an item is focused ?
Thank you.
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Top

Postby plusminus » Sat Dec 15, 2007 2:53 pm

Hey Lex,

the following code-snippet does what you want to achive:

I placed in the onCreate(...)-method of the ListActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         this.getListView().setOnItemSelectedListener(new OnItemSelectedListener(){
  2.                         @Override
  3.                         public void onItemSelected(AdapterView parent, View v,
  4.                                         int position, long id) {
  5.                                 showAlert("Selection Changed...", "Position: " + position, "OK", false);
  6.                         }
  7.  
  8.                         @Override
  9.                         public void onNothingSelected(AdapterView arg0) {
  10.                                 // Do Nothing
  11.                         }
  12.         });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Did it help you :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Lex » Sun Dec 16, 2007 8:04 pm

It doesn't work, OnItemSelectedListener is not recognised as an existing method.
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby plusminus » Sun Dec 16, 2007 8:18 pm

Hello Lex,

what do you mean with "it didn't work".
Didn't compile or didn't lead to awaited result :?:

What was the error :?:
With me it requires the following imports to work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.view.View;
  2. import android.widget.AdapterView;
  3. import android.widget.AdapterView.OnItemSelectedListener;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



This is my full working Code (no Compile-Errors):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.testproject;
  2.  
  3. import android.app.ListActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.Spinner;
  8. import android.widget.TextView;
  9. import android.widget.AdapterView.OnItemSelectedListener;
  10.  
  11.  
  12. public class TestLayout extends ListActivity {
  13.  
  14.     @Override
  15.     public void onCreate(Bundle icicle){
  16.         super.onCreate(icicle);
  17.         TextView v = new TextView(this);
  18.         Spinner sp = new Spinner(this);
  19.         sp.setOnItemSelectedListener(new OnItemSelectedListener(){
  20.                         @Override
  21.                         public void onItemSelected(AdapterView parent, View v,
  22.                                         int position, long id) {
  23.                                 showAlert("Selection Changed...", "Position: " + position, "OK", false);
  24.                         }
  25.  
  26.                         @Override
  27.                         public void onNothingSelected(AdapterView arg0) {
  28.                                
  29.                         }
  30.         });
  31.         this.setContentView(sp);
  32.     }
  33. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests