Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 20th, 2013  |  syntax: Java  |  size: 2.08 KB  |  views: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.example.exampletodo;
  2.  
  3. import android.R;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.view.KeyEvent;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.View.OnKeyListener;
  11. import android.widget.EditText;
  12. import android.widget.Button;
  13. import android.widget.ListView;
  14. import java.util.ArrayList;
  15. import android.widget.ArrayAdapter;
  16.  
  17. public class Main extends Activity implements OnClickListener, OnKeyListener {
  18.  
  19.     EditText txtItem;
  20.     Button btnAdd;
  21.     ListView listItems;
  22.  
  23.     ArrayList<String> toDoItems;
  24.     ArrayAdapter<String> aa;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.main);
  30.  
  31.         txtItem = (EditText)findViewById(R.id.txtItem);
  32.         btnAdd = (Button)findViewById(R.id.btnAdd);
  33.         listItems = (ListView)findViewById(R.id.listItems);
  34.  
  35.         btnAdd.setOnClickListener(this);
  36.         txtItem.setOnKeyListener(this);
  37.  
  38.         toDoItems = new ArrayList<String>();
  39.         aa = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, toDoItems);
  40.         listItems.setAdapter(aa);
  41.     }
  42.  
  43.     private void addItem(String item){
  44.         if(item.length() > 0){
  45.             this.toDoItems.add(item);
  46.             this.aa.notifyDataSetChanged();
  47.             this.txtItem.setText("");
  48.         }
  49.     }
  50.  
  51.     @Override
  52.     public boolean onCreateOptionsMenu(Menu menu) {
  53.         // Inflate the menu; this adds items to the action bar if it is present.
  54.         getMenuInflater().inflate(R.menu.main, menu);
  55.         return true;
  56.     }
  57.  
  58.     public void onClick(View v){
  59.         if(v == this.btnAdd){
  60.             this.addItem(this.txtItem.getText().toString());
  61.         }
  62.     }
  63.  
  64.     public boolean onKey(View v, int keyCode, KeyEvent event){
  65.         if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
  66.             this.addItem(this.txtItem.getText().toString());
  67.         }
  68.         return false;
  69.     }
  70.  
  71. }
clone this paste RAW Paste Data