从todoList中删除项目

时间:2010-04-08 12:13:41

标签: android

需要严肃的帮助Android todolist任何人都可以帮助请。 我试图删除列表中插入的最后一项由于某些原因它不起作用。如果您能看到以下内容,请提供帮助:

ToDoList.java

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;
}

}

1 个答案:

答案 0 :(得分:2)

我看不到您设置了btnRemove.onClickListener(this);

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