tags:

views:

22

answers:

1

Need serious help with android todolist anyone could help please. I am trying to remove the last item inserted in the list it doesn't work for some reasons. please help if you can see the below:

public class ToDoList extends Activity implements OnClickListener, OnKeyListener {

Button btnRemove;
 ArrayList<String> todoItems;
  ArrayAdapter<String> aa;
  ListView myListView ;
  EditText myEditText ;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

// Inflate your view
  setContentView(R.layout.main);

  // Get references to UI widgets
   myListView = (ListView)findViewById(R.id.myListView);
   myEditText = (EditText)findViewById(R.id.myEditText);  

  // Create the ArrayList and the ArrayAdapter
 todoItems = new ArrayList<String>();
  aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,todoItems);

  // Bind the listview to the array adapter
  myListView.setAdapter(aa);

  btnRemove = (Button)findViewById(R.id.btnRemove);

// Add key listener to add the new todo item
  // when the middle D-pad button is pressed.
  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) {
        // Add the new todo item, and clear the input text box
        todoItems.add(0, myEditText.getText().toString());
        myEditText.setText("");
        aa.notifyDataSetChanged();
        return true;
      }
    return false;
  }
});

}

@Override public void onClick(View v) { if(v == btnRemove){ if(todoItems.size() > 0){ todoItems.remove(todoItems.size() - 1); aa.notifyDataSetChanged(); }

} } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub return false; }

}

A: 

I can't see that you set btnRemove.onClickListener(this);

btnRemove = (Button)findViewById(R.id.btnRemove);
btnRemove.onClickListener(this);  // <= fix
PHP_Jedi