onKeyDown event problems

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

onKeyDown event problems

Postby Nitinkcv » Thu Nov 29, 2007 3:17 pm

Hi all,

Currently im displaying a simple ListActivity which displays three texts in the order:
Google
eBay
Yahoo

what im trying to implement is that, when the index comes to Yahoo and then when i click the down button i should be able to go to the first section which is Google. Similarly when its at Google and then i click the top button in the emulator it should go to Yahoo.

I was able to do this. <b>But<b> the problem is that the onKeyDown event is not called when i try to go eBay from Google also to Yahoo from eBay. <b>But</b> when i hit the down key when the focus is at Yahoo, the onKeyDown event is triggered and the focus takes it to the First item which is Google.

Similar is the case when i try to go from bottom to top.

my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.ListActivity;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.view.KeyEvent;
  6.  
  7. import android.widget.ArrayAdapter;
  8.  
  9.  
  10.  
  11. public class SimpleListScreen extends ListActivity {
  12.  
  13.        
  14.  
  15.          private String[] mStrings = new String[] { "Android", "Google", "Eclipse" };
  16.  
  17.          private int mStrings_length= mStrings.length-1;
  18.  
  19.          
  20.  
  21.         /** Called when the activity is first created. */
  22.  
  23.         @Override
  24.  
  25.         public void onCreate(Bundle icicle) {
  26.  
  27.                 super.onCreate(icicle);
  28.  
  29.  
  30.  
  31.                 // Create an array of Strings, that will be put to our ListActivity
  32.  
  33.                
  34.  
  35.  
  36.  
  37.                 // Create an ArrayAdapter, that will actually make the Strings above
  38.  
  39.                 // appear in the ListView
  40.  
  41.  
  42.  
  43.                 this.setListAdapter(new ArrayAdapter<String>(this,
  44.  
  45.                                 android.R.layout.simple_list_item_1, mStrings));
  46.  
  47.  
  48.  
  49.         }
  50.  
  51.  
  52.  
  53.         @Override
  54.  
  55.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  56.  
  57.                 // TODO Auto-generated method stub
  58.  
  59.                                
  60.  
  61.                 switch (keyCode) {
  62.  
  63.                 case KeyEvent.KEYCODE_DPAD_DOWN:
  64.  
  65.                        
  66.  
  67.                         moveToTop();
  68.  
  69.                         return true;
  70.  
  71.                 case KeyEvent.KEYCODE_DPAD_UP:
  72.  
  73.                        
  74.  
  75.                         moveToBottom();
  76.  
  77.                         return true;
  78.  
  79.                 }
  80.  
  81.                
  82.  
  83.                 return false;
  84.  
  85.         }
  86.  
  87.        
  88.  
  89.         public void moveToTop()
  90.  
  91.         {
  92.  
  93.                 int selectionRowID = (int) this.getSelectionRowID();
  94.  
  95.                 if(selectionRowID==(mStrings_length)){
  96.  
  97.                        
  98.  
  99.                         this.setSelection(0);
  100.  
  101.                 }
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         public void moveToBottom()
  108.  
  109.         {
  110.  
  111.                 int selectionRowID = (int) this.getSelectionRowID();
  112.  
  113.                 if(selectionRowID==0){
  114.  
  115.                        
  116.  
  117.                         this.setSelection(mStrings_length);
  118.  
  119.                 }
  120.  
  121.         }
  122.  
  123. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


What could be the reason?

Thanks,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Top

Postby plusminus » Thu Nov 29, 2007 3:56 pm

Hello Nitinkcv,

hm, that is really strange... seems like a bug to me.. :!:
A dirty workaround would applying a KexListener to the ListView itself instead to the Activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 this.getListView().setKeyListener(new OnKeyListener() {
  2.                         // @Override
  3.                         public boolean onKey(View view, int keyCode, KeyEvent event) {
  4.                                 switch (keyCode) {
  5.                                         case KeyEvent.KEYCODE_DPAD_DOWN:
  6.                                                 // tap
  7.                                                 return true;
  8.                                         case KeyEvent.KEYCODE_DPAD_UP:
  9.                                                 // tap
  10.                                                 return true;
  11.                                 }
  12.                                 return false;
  13.                         }
  14.                 });
Parsed in 0.033 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

KeyDown not working

Postby Nitinkcv » Fri Nov 30, 2007 8:44 am

Hi plusMinus,

Still no luck. This time the keyDown event is triggered everytime but the focus(index) doesnot change. What i mean is that on clicking the down key the focus doesnt go to the next listitem, instead it stays there.

But when im at the 0th index and press the up key the focus goes to the last index. Similar is the case when the index is at the last item.

Im including the code u had posted in the public void onCreate(Bundle icicle) method.

On debugging i found that inside the switch both the return true as well as the return false was being executed :!:

Thanks,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests