ListView Onkey down problem

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

ListView Onkey down problem

Postby milkround » Thu Jun 18, 2009 10:23 am

Hi folks,

There seems to be a recurring problem for new users regarding servicing of ListView onKeyDown keypresses.

My code:-

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class test2 extends ListActivity implements android.view.View.OnKeyListener {
  2.  
  3.         TextView selection;
  4.  
  5.         Weekfile  Week;
  6.  
  7.     public int[]    tag;
  8.  
  9.     int posit=0;
  10.  
  11.     int selected;
  12.  
  13.     ListView myListView;
  14.  
  15.  
  16.  
  17.         @Override
  18.  
  19.         public void onCreate(Bundle icicle) {
  20.  
  21.             Week = new Weekfile();
  22.  
  23.             Week.Openfile("wk200922");
  24.  
  25.             //create data for listview
  26.  
  27.  
  28.  
  29.             super.onCreate(icicle);
  30.  
  31.             setContentView(R.layout.main);  
  32.  
  33.             WeatherAdapter weatherAdapter = new WeatherAdapter(
  34.  
  35.                                 this,
  36.  
  37.                                 tag,
  38.  
  39.                                 Week);      
  40.  
  41.         setListAdapter( weatherAdapter);    
  42.  
  43.  
  44.  
  45.         myListView = getListView();
  46.  
  47.         myListView.setChoiceMode(1);
  48.  
  49.         myListView.setFocusableInTouchMode(true);
  50.  
  51.         myListView.setFocusable(true);
  52.  
  53.         myListView.setOnKeyListener(new OnKeyListener() {  //<<<<<<<<< objects about setOnKeyListener here
  54.  
  55.                 //@Override
  56.  
  57.                 public boolean onKey(DialogInterface dialog, int keyCode,
  58.  
  59.                                         KeyEvent event) {
  60.  
  61.                                 // TODO Auto-generated method stub
  62.  
  63.                 System.out.println("KEYDOWN " + keyCode);
  64.  
  65.                                 return false;
  66.  
  67.                         }
  68.  
  69.        });        
  70.  
  71.        
  72.  
  73.         }
  74.  
  75.        
  76.  
  77.         @Override
  78.  
  79.         public boolean onKeyDown(int keyCode,           //<<<<<<<<<<<< this gets left and right events
  80.  
  81.                         KeyEvent event){
  82.  
  83.         System.out.println("KEYDOWN " + keyCode);
  84.  
  85.         return (true);
  86.  
  87.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



The last onKeyDown marked with //<<<< accepts the KEYCODE_DPAD_LEFT & KeyEvent.KEYCODE_DPAD_RIGHT: events
but NOT the KEYCODE_DPAD_UP & KeyEvent.KEYCODE_DPAD_DOWN:
events so clearly the ListView is processing these.

In an attempt to replace the ListView.OnKey event I tried to setup a

myListView.setOnKeyListener(new OnKeyListener() but the Eclipse tells me that
The method setOnKeyListener(...) in type View is not applicable for the arguments??
What am I doing wrong??
If I can't setup the OnKeyListener here, then where??
------------------------------------------------------------------------------------------------------------------------------
A thought - A lot of these type of starter bugs would be prevented if there was a Wizard to help set up a basic screen - like a Base Adapter with onItemClick , onKey , setFocusableInTouchMode, setFocusable(true)in the right place so they work. (selectable via checkboxes).

Can anyone point me to some reference material that explains how to get FocusableInTouchMode working?
I understand this is frowned upon but the only people who will use my app will be people who bought the device to use my app.

Steve
milkround
Developer
Developer
 
Posts: 34
Joined: Sat May 02, 2009 2:29 pm
Location: UK & Malta

Top

Postby WarrenFaith » Fri Jun 19, 2009 8:54 am

I understand this is frowned upon but the only people who will use my app will be people who bought the device to use my app.

Wow... 300 euro or more just to use your application....

back to topic:
Sorry but you post is a mess... Please clear out what exactly isnt working and how should it work?!

If I try your code, I got an error on the onKey() method. Change it to that and it should work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myListView.setOnKeyListener(new OnKeyListener() {
  2.     @Override
  3.     public boolean onKey(View v, int keyCode, KeyEvent event) {
  4.         if (event.getAction() == KeyEvent.ACTION_DOWN) {
  5.             Log.i("keyevent", "i am down!");
  6.         }
  7.         return true;
  8.     }
  9. });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

List View OnKey

Postby milkround » Fri Jun 19, 2009 4:22 pm

Hi,
Thanks for your help. I think I am doing something really stupid

The compiler still objects to your code.

The exact errors are:-

The type new DialogInterface.OnKeyListener(){} must implement the inherited abstract method DialogInterface.OnKeyListener.onKey(DialogInterface, int, KeyEvent)
test2.java Test3/src/com/example/test2 line 103 Java Problem

The method setOnKeyListener(View.OnKeyListener) in the type View is not applicable for the arguments (new DialogInterface.OnKeyListener(){})
test2.java Test3/src/com/example/test2 line 103 Java Problem

The method onKey(View, int, KeyEvent) of type new DialogInterface.OnKeyListener(){} must override a superclass method
test2.java Test3/src/com/example/test2 line 105 Java Problem

I see the words I don't understand what is required.

The reason I want this is the the action of the up/down button need to be specific to my system
on row type A go down to next type A
on Row type B go down one row

Many thanks steve
In the mean time I am changing screen values so some interaction
milkround
Developer
Developer
 
Posts: 34
Joined: Sat May 02, 2009 2:29 pm
Location: UK & Malta

Postby WarrenFaith » Fri Jun 19, 2009 7:39 pm

Which SDK version are you using?
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

SDK Version

Postby milkround » Fri Jun 19, 2009 10:14 pm

android-sdk-windows-1.5_r2

Eclipse Platform

Version: 3.4.2
Build id: M20090211-1700

Steve
milkround
Developer
Developer
 
Posts: 34
Joined: Sat May 02, 2009 2:29 pm
Location: UK & Malta

Postby WarrenFaith » Sat Jun 20, 2009 9:48 pm

hm... ok I don't know why you use the interface "android.view.View.OnKeyListener" for your activity class... maybe thats the reason.

Another way: zip your project and attach it here, i will have a deeper look
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Top

Two Problems

Postby milkround » Sun Jun 21, 2009 9:16 pm

Hi,
I modified an earlier test program - Your comments would be appreciated.

Problem - Commented out SetOnKeyListener (My original problem)
-------------------------------------------------------
/*
myListView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
Log.i("keyevent", "i am down!");
return false;
}
});
*/
remove comments - will not compile (without the "implements android.view.View.OnKeyListener")

[2009-06-21 20:59:13 - Test2] Conversion to Dalvik format failed with error 1
[2009-06-21 21:00:27 - Test2] no classfiles specified
[2009-06-21 21:00:27 - Test2] Conversion to Dalvik format failed with error 1

Slightly different code but basically the same problem ( I believe)

Many thanks in anticipation!
Steve
Attachments
Test2.zip
Simple test program with two problems
(46.31 KiB) Downloaded 66 times
milkround
Developer
Developer
 
Posts: 34
Joined: Sat May 02, 2009 2:29 pm
Location: UK & Malta

Re: ListView Onkey down problem

Postby riteshambastha » Wed Sep 21, 2011 1:10 am

//complete working code

package com.iws.todolist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);

final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener(){

public boolean onKey(View v, int keyCode, KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_DOWN)
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
todoItems.add(0,myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}


});

}
}
riteshambastha
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Sep 21, 2011 1:08 am

Re: ListView Onkey down problem

Postby Mounika » Thu Jan 19, 2012 1:33 pm

Hi every one..
I am very new to android platform..
and i tried to execute the example..
but getting the same error.. :(
i.e: The method setOnKeyListener(View.OnKeyListener) in the type View is not applicable for the arguments (new DialogInterface.OnKeyListener(){})

when setting the onkeylistener to the edittext field..
help me out..

Regards,

Mounika
Mounika
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 19, 2012 1:26 pm

Re: ListView Onkey down problem

Postby Mounika » Thu Jan 19, 2012 1:45 pm

I got it corrected just have to change one import statement..

from
import android.content.DialogInterface.OnClickListener;

to
import android.view.View.OnKeyListener;
Mounika
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 19, 2012 1:26 pm

Re: ListView Onkey down problem

Postby Phyll » Thu Jan 19, 2012 2:13 pm

Hi All,

This is a pretty old thread but this is how I monitor the Enter key on an EditText and maybe it will help. I don't use implements onkeylistener in the class header at all.

Code: Select all
    In the onCreate after setting the EditText et:

            et.setOnEditorActionListener(mSendListener);

    The listener down with the rest of the methods:

    private TextView.OnEditorActionListener mSendListener =
        new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            // If the action is a key-up event on the return key, send the message
            if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
                if (tt.length() == 0) {
                    Toast.makeText(view.getContext(), "No Message", Toast.LENGTH_SHORT).show();
                    return false;
                    }//check for connect
                SendIt();
                }//return key 
            return true;     
        }
    };


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: ListView Onkey down problem

Postby Mounika » Thu Jan 19, 2012 2:27 pm

but its not executing in emulator.. :(

its showing it has stopped...
Mounika
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 19, 2012 1:26 pm

Re: ListView Onkey down problem

Postby Phyll » Thu Jan 19, 2012 2:46 pm

Hi Mounika,

I might be able to help but I don't know exactly what the problem is. Looks like you have an activity running as a dialog or something. Please give us a little more info and I'll see if I can sort it out.

Help us to help you.

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: ListView Onkey down problem

Postby Mounika » Fri Jan 20, 2012 6:00 am

Hi Phyll,

I am just posting my code snippet here..
Can you plz help me out where i'm going wrong...

Code: Select all
package com.todo;

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.view.View.OnKeyListener;

public class ToDoList extends ListActivity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);      
      setContentView(R.layout.main);

      ListView myListView = (ListView) findViewById(R.id.myListView);
      final EditText myEditText = (EditText) findViewById(R.id.myEditText);

      final ArrayList<String> todoItems = new ArrayList<String>();      
      final ArrayAdapter<String> aryAdapter;
      aryAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, todoItems);      
      myListView.setAdapter(aryAdapter);
      
      myEditText.setOnKeyListener(new OnKeyListener() {
         public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
               if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                  todoItems.add(0, myEditText.getText().toString());
                  aryAdapter.notifyDataSetChanged();
                  myEditText.setText("");
                  return true;
               }
            return false;
         }
      });
   }
}


and xml file is as follows:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint=""
        android:text="@string/txt_list" />

    <ListView
        android:id="@+id/myListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


when i execute thiss in emulator i get a msg as:
"Unexpectedly TO-Do has stopped"

and logcat shows as follos:
Code: Select all
01-20 12:34:06.627: D/dalvikvm(610): Not late-enabling CheckJNI (already on)
01-20 12:34:11.697: D/AndroidRuntime(610): Shutting down VM
01-20 12:34:11.697: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-20 12:34:11.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
01-20 12:34:11.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.todo/com.todo.ToDoList}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.os.Looper.loop(Looper.java:137)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-20 12:34:11.818: E/AndroidRuntime(610):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 12:34:11.818: E/AndroidRuntime(610):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 12:34:11.818: E/AndroidRuntime(610):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 12:34:11.818: E/AndroidRuntime(610):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 12:34:11.818: E/AndroidRuntime(610):    at dalvik.system.NativeStart.main(Native Method)
01-20 12:34:11.818: E/AndroidRuntime(610): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-20 12:34:11.818: E/AndroidRuntime(610):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.Activity.setContentView(Activity.java:1835)
01-20 12:34:11.818: E/AndroidRuntime(610):    at com.todo.ToDoList.onCreate(ToDoList.java:22)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.Activity.performCreate(Activity.java:4465)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 12:34:11.818: E/AndroidRuntime(610):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-20 12:34:11.818: E/AndroidRuntime(610):    ... 11 more
01-20 12:34:15.877: I/Process(610): Sending signal. PID: 610 SIG: 9


so what might be the actual reason for this prob..??
Waiting for ur reply...

Regards,
Mounika
Mounika
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 19, 2012 1:26 pm

Re: ListView Onkey down problem

Postby Phyll » Fri Jan 20, 2012 12:02 pm

Hi Mounika,

You need to move those global variables outside of the onCreate method. Also missing an override on the onKey event. This code worked fine for me:

Code: Select all
    ArrayList<String> todoItems = new ArrayList<String>();     
    ArrayAdapter<String> aryAdapter;
    ListView myListView;
    EditText myEditText;
   
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        myListView = (ListView) findViewById(R.id.myListView);
        myEditText = (EditText) findViewById(R.id.myEditText);
        aryAdapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, todoItems);     
        myListView.setAdapter(aryAdapter);
        myEditText.setOnKeyListener(new OnKeyListener() {
           
           @Override   
           public boolean onKey(View v, int keyCode, KeyEvent event) {
              if (event.getAction() == KeyEvent.ACTION_DOWN)
                 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                    todoItems.add(0, myEditText.getText().toString());
                    aryAdapter.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                 }
              return false;
           }

        });


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests