Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static ArrayList<String> playerList = new ArrayList<String>(10);
- ListView employeeList;
- ArrayAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.addremove);
- final MyAdapter adapter = new MyAdapter(this, R.layout.listview_content, playerList);
- ListView employeeList = (ListView) findViewById(R.id.namelistview);
- employeeList.setAdapter(adapter);
- if(playerList.size() < 10) {
- Button confirm = (Button) findViewById(R.id.add);
- confirm.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- EditText playername = (EditText) findViewById(R.id.userinput);
- playerList.add(playername.getText().toString());
- adapter.notifyDataSetChanged();
- playername.setText("");
- }});
- } else {
- // do nothing
- }
- Button play = (Button) findViewById(R.id.playnow);
- play.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent( demo.AddRemove.this, demo.PasswActivity.class);
- intent.putStringArrayListExtra("KEY", playerList);
- startActivity(intent);
- }});
- }
- class MyAdapter extends ArrayAdapter<String>{
- private OnClickListener listener;
- public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
- super(context, textViewResourceId, objects);
- listener = new OnClickListener(){
- public void onClick(View v){
- playerList.remove(v.getTag());
- notifyDataSetChanged();
- };
- };
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.listview_content, null);
- TextView myTextView1 = (TextView)convertView.findViewById(R.id.playername);
- myTextView1.setText(getItem(position));
- Button remove = (Button)convertView.findViewById(R.id.remove);
- remove.setTag(getItem(position));
- remove.setOnClickListener(listener);
- return convertView;
- }
- }
- @Override
- public void onSaveInstanceState(Bundle outState) {
- // Save UI state changes to the savedInstanceState.
- // This bundle will be passed to onCreate if the process is
- // killed and restarted.
- outState.putStringArrayList("Playlist", playerList );
- super.onSaveInstanceState(outState); }
- @Override
- public void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- // Restore UI state from the savedInstanceState.
- // This bundle has also been passed to onCreate.
- playerList = savedInstanceState.getStringArrayList("Playlist");
- }
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement