tpbolon

Membuat Spinner dengan data dinamis berupa String Array

Jun 18th, 2020
951
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.spinnerkedua;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Spinner;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends Activity {
  13.     private Spinner splistmakanan;
  14.     private String[] daftarmakanan={"Nasi Goreng","Mie Goreng", "Mie Rebus",
  15.             "Sate Padang", "Nasi Goreng Spesial"};
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         splistmakanan=(Spinner)findViewById(R.id.spmakanan);
  21.         final ArrayAdapter<String> Adaptor=new ArrayAdapter<String>(this,
  22.                 android.R.layout.simple_spinner_item,daftarmakanan);
  23.         splistmakanan.setAdapter(Adaptor);
  24.        
  25.         splistmakanan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  26.  
  27.             @Override
  28.             public void onItemSelected(AdapterView<?> adapterView, View view,
  29.                     int i, long l) {
  30.                 // TODO Auto-generated method stub
  31.                 Toast.makeText(MainActivity.this,
  32.                         "Saya memesan "+ Adaptor.getItem(i),
  33.                         Toast.LENGTH_SHORT).show();
  34.                
  35.             }
  36.  
  37.             @Override
  38.             public void onNothingSelected(AdapterView<?> arg0) {
  39.                 // TODO Auto-generated method stub
  40.                
  41.             }
  42.         });
  43.     }
  44.  
  45.  
  46.     @Override
  47.     public boolean onCreateOptionsMenu(Menu menu) {
  48.         // Inflate the menu; this adds items to the action bar if it is present.
  49.         getMenuInflater().inflate(R.menu.main, menu);
  50.         return true;
  51.     }
  52.    
  53. }
RAW Paste Data