SHARE
TWEET

Untitled

a guest Apr 22nd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package stts.edu.week9room;
  2.  
  3. import android.arch.persistence.room.Room;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.EditText;
  9. import android.widget.Spinner;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13.  
  14. public class EditActivity extends AppCompatActivity {
  15.     private AppDatabase db;
  16.     private ArrayList<String> arrid = new ArrayList<String>();
  17.     private Spinner spin;
  18.     private ArrayAdapter<String> spinadapter;
  19.     private EditText etnama, etmerk, etHarga;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_edit);
  25.  
  26.         db = Room.databaseBuilder(getApplicationContext(),AppDatabase.class, "barangdb").build();
  27.         spin = findViewById(R.id.spinner);
  28.         spinadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrid);
  29.         spin.setAdapter(spinadapter);
  30.         etnama = findViewById(R.id.ed_namaBarang);
  31.         etmerk = findViewById(R.id.ed_merkBarang);
  32.         etHarga = findViewById(R.id.ed_hargaBarang);
  33.  
  34.         loadSpinner();
  35.     }
  36.  
  37.     public void loadSpinner(){
  38.         new AsyncTask<Void, Void, ArrayList<Barang>>() {
  39.             //didalam doInBackground tidak boleh akses tampilan
  40.             @Override
  41.             protected ArrayList<Barang> doInBackground(Void... voids) {
  42.                 ArrayList<Barang> hasil = new ArrayList<>();
  43.                 hasil.addAll(Arrays.asList(db.barangDAO().selectAllBarangs()));
  44.                 return hasil;
  45.             }
  46.  
  47.             @Override
  48.             protected void onPostExecute(ArrayList<Barang> barangs) {
  49.                 arrid.clear();
  50.                 for(Barang b : barangs){
  51.                     arrid.add(String.valueOf(b.getId_barang()) + " - " + String.valueOf(b.getNama_barang()));
  52.                 }
  53.                 spinadapter.notifyDataSetChanged();
  54.             }
  55.         }.execute();
  56.     }
  57.  
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top