jeudi 12 décembre 2019

Is there a way around doing 2000 else if statements in this?

I am creating a check in barcode app for my business in Android Studio. There are 2000 of these. A member types in their 6 digit membership number into editText, clicks Save, which triggers saveToMain below. My plan was to just use else if for each membership card. Kotlin doesn't like this and keeps crashing after about 200 of these. I'm very new to coding and literally learned the language just for this app, so I know approximately nothing, so, this might be crazy and there may be a much easier way. I've been searching on the Internet for the past two hours and can't seem to find anything with this same situation. You cannot use an array somehow for this, correct?

The R.drawable.a0001 files below are the barcode images corresponding to their membership number.

fun alertDiag() {
    val alertDialog =
        AlertDialog.Builder(this).create()
    alertDialog.setTitle("No Card Entered")
    alertDialog.setMessage("Please use 'Current Membership Card' for your previously saved Membership Card.")
    alertDialog.setButton(
        AlertDialog.BUTTON_POSITIVE,
        "OK"
    ) { dialog, which ->

    }

    alertDialog.show()
}


fun saveToMain(view: View) {
    val editText = findViewById<EditText>(R.id.editText)
    val message = editText.text.toString()
    val image2 = findViewById<View>(R.id.imageView2)

    if (message == "") {
        alertDiag() }
    else if (message == "100001") {image2.setBackgroundResource(R.drawable.a0001);saveToStorage(view);goToGallery(view)}
    else if (message == "100002") {image2.setBackgroundResource(R.drawable.a0002);saveToStorage(view);goToGallery(view)}
    else if (message == "100003") {image2.setBackgroundResource(R.drawable.a0003);saveToStorage(view);goToGallery(view)}
    else if (message == "100004") {image2.setBackgroundResource(R.drawable.a0004);saveToStorage(view);goToGallery(view)}
    else {
        image2.setBackgroundResource(R.drawable.notfound)
    }
}

else if

Aucun commentaire:

Enregistrer un commentaire