View Single Post
  #1 (permalink)  
Old December 30th, 2011, 08:53 AM
harrison57 harrison57 is offline
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 2 To Do List setOnKeyListener Problem

Hi,

I tried to execute the code mentioned in the book, but i keep getting the error

Multiple markers at this line
- The method setOnKeyListener(View.OnKeyListener) in the type View is not applicable for the arguments
(new DialogInterface.OnKeyListener(){})
- The type new DialogInterface.OnKeyListener(){} must implement the inherited abstract method
DialogInterface.OnKeyListener.onKey(DialogInterfac e, int, KeyEvent)



package com.androiddevelopment.todolist;

import java.util.ArrayList;

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


public class ToDoListActivity 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);

// Bind the array adapter to the listview
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;
}
});
}

}
Reply With Quote