Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2012
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. private static ArrayList<String> playerList = new ArrayList<String>(10);
  2.     ListView employeeList;
  3.     ArrayAdapter adapter;
  4.  
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.     // TODO Auto-generated method stub
  8.     super.onCreate(savedInstanceState);
  9.     setContentView(R.layout.addremove);
  10.    
  11.     final MyAdapter adapter = new MyAdapter(this, R.layout.listview_content, playerList);
  12.     ListView employeeList = (ListView) findViewById(R.id.namelistview);
  13.     employeeList.setAdapter(adapter);
  14.    
  15.    
  16.     if(playerList.size() < 10) {
  17.  
  18.     Button confirm = (Button) findViewById(R.id.add);
  19.     confirm.setOnClickListener(new OnClickListener() {
  20.     public void onClick(View v) {
  21.     EditText playername = (EditText) findViewById(R.id.userinput);
  22.     playerList.add(playername.getText().toString());
  23.     adapter.notifyDataSetChanged();
  24.     playername.setText("");
  25.  
  26.     }});
  27.     } else {
  28.            // do nothing
  29.         }
  30.  
  31.  
  32.     Button play = (Button) findViewById(R.id.playnow);
  33.     play.setOnClickListener(new OnClickListener() {
  34.     public void onClick(View v) {
  35.     Intent intent = new Intent( demo.AddRemove.this, demo.PasswActivity.class);
  36.     intent.putStringArrayListExtra("KEY", playerList);
  37.     startActivity(intent);
  38.  
  39.     }});
  40.     }
  41.     class MyAdapter extends ArrayAdapter<String>{
  42.         private OnClickListener listener;
  43.         public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
  44.             super(context, textViewResourceId, objects);
  45.             listener = new OnClickListener(){
  46.                 public void onClick(View v){
  47.                 playerList.remove(v.getTag());
  48.                 notifyDataSetChanged();
  49.  
  50.                 };
  51.                 };
  52.                 }
  53.  
  54.  
  55.         public View getView(int position, View convertView, ViewGroup parent) {
  56.             if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.listview_content, null);
  57.  
  58.             TextView myTextView1 = (TextView)convertView.findViewById(R.id.playername);
  59.             myTextView1.setText(getItem(position));
  60.             Button remove = (Button)convertView.findViewById(R.id.remove);
  61.             remove.setTag(getItem(position));
  62.             remove.setOnClickListener(listener);
  63.             return convertView;
  64.         }
  65.     }
  66.    
  67.      @Override
  68.      public void onSaveInstanceState(Bundle outState) {
  69.        // Save UI state changes to the savedInstanceState.
  70.        // This bundle will be passed to onCreate if the process is
  71.        // killed and restarted.
  72.          outState.putStringArrayList("Playlist", playerList );
  73.  
  74.         super.onSaveInstanceState(outState); }
  75.  
  76.          
  77.         @Override
  78.         public void onRestoreInstanceState(Bundle savedInstanceState) {
  79.           super.onRestoreInstanceState(savedInstanceState);
  80.           // Restore UI state from the savedInstanceState.
  81.           // This bundle has also been passed to onCreate.
  82.             playerList = savedInstanceState.getStringArrayList("Playlist");
  83.            
  84.  
  85.         }
  86.        
  87.  
  88.         protected void onPause() {
  89.             // TODO Auto-generated method stub
  90.             super.onPause();
  91.             finish();
  92.         }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement