如何在自动完成setOnItemSelected条件下管理按钮setEnable?

时间:2019-07-04 17:29:59

标签: android firebase-realtime-database autocomplete android-button enable-if

我正在建立我的家庭作业项目,您能解决我的问题吗?

我已经使用微调器进行了部落相似的方法,但是它不起作用

public class Tambah_Peminjaman extends Activity {
    Button btnPinjam;
    EditText edIdPinjam, edNamaMember, edNamaBuku, edTanggalPinjam, edBulanPinjam, edTahunPinjam;
    DatabaseReference dbPinjaman, database;
    Spinner jdSpinner;
    AutoCompleteTextView auto;

    String kdbuku;



    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pinjam_buku);
        edIdPinjam = findViewById(R.id.edidpinjam);
        edNamaMember = findViewById(R.id.ednamamember);
        edNamaBuku = findViewById(R.id.ednamabuku);
        edTanggalPinjam = findViewById(R.id.edtgl);
        edBulanPinjam = findViewById(R.id.edbln);
        edTahunPinjam = findViewById(R.id.edthn);
        auto = findViewById(R.id.actv);
        jdSpinner = findViewById(R.id.spinner);
        btnPinjam = findViewById(R.id.buttonPinjam);
        btnPinjam.setEnabled(false);
        dbPinjaman = FirebaseDatabase.getInstance().getReference("peminjaman");
        database = FirebaseDatabase.getInstance().getReference();


        final ArrayAdapter<String> autoComplete = new ArrayAdapter<>(Tambah_Peminjaman.this, android.R.layout.simple_list_item_1);

        final List<String> jdl = new ArrayList<String>();
        kdbuku = jdl.get();


        database.child("buku").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                final List<String> judul = new ArrayList<String>();

                for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()){
                    String suggestion  = suggestionSnapshot.child("judul_buku").getValue(String.class);
                    judul.add(suggestion);
                    autoComplete.add(suggestion);
                }
                Spinner judulSpiner = (Spinner)findViewById(R.id.spinner);
                ArrayAdapter<String> judulsAdapter = new ArrayAdapter<String>(Tambah_Peminjaman.this, android.R.layout.simple_spinner_item, judul );
                judulsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                judulSpiner.setAdapter(judulsAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        AutoCompleteTextView ACTV = (AutoCompleteTextView)findViewById(R.id.actv);
        ACTV.setAdapter(autoComplete);
        ACTV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                kdbuku = parent.getItemAtPosition(position).toString();

                submit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btnPinjam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pinjamBuku();
            }
        });
    }

    private void pinjamBuku() {
        // TODO: Implement this Method
        //String edidpinjam = edIdPinjam.getText().toString();
        String ednamamember = edNamaMember.getText().toString();
        String ednamabuku = edNamaBuku.getText().toString();
        String edtglpinjam = edTanggalPinjam.getText().toString();
        String edblnpinjam = edBulanPinjam.getText().toString();
        String edthnpinjam = edTahunPinjam.getText().toString();
        String autoComplete = auto.getText().toString();

        if (!TextUtils.isEmpty(autoComplete) ){
            String edipinjam = dbPinjaman.push().getKey();

            Peminjaman pinjam = new Peminjaman(edipinjam, ednamamember,autoComplete, edtglpinjam );
            dbPinjaman.child(edipinjam).setValue(pinjam);

            edIdPinjam.setText("");
            edNamaMember.setText("");
            edNamaBuku.setText("");
            edTanggalPinjam.setText("");
            edBulanPinjam.setText("");
            edTahunPinjam.setText("");
            auto.setText("");

            Toast.makeText(this, "Buku Berhasil Dipinjam", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Masukan Nomor Pinjam", Toast.LENGTH_LONG).show();
        }
    }

    public void submit(){
        if (kdbuku.equals(database.getParent().child("judul_buku"))){
            btnPinjam.setEnabled(true);
        }
    }

    public static Intent getActIntent(Activity activity){
        return new Intent(activity, Tambah_Peminjaman.class);
    }
}

这是我的代码,我仍然学习编程以免一团糟

0 个答案:

没有答案