我有一个if语句,里面有相同的代码

时间:2018-01-22 02:38:02

标签: java

所以......我有一个像这样的java代码

            public void onClick(View arg0) {
            String sebelumnrp = text1.getText().toString();
            int nrp = Integer.parseInt(sebelumnrp);
            String idb = text11.getText().toString();
            int id = Integer.parseInt(idb);
            String sebelumjob = text2.getText().toString();
            int codejob = Integer.parseInt(sebelumjob);
            if (nrp == 80110005){
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String nama = "AKHMAD ROZA′I";
                if(codejob == 100) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama +"' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Cuti(Tahunan, Site)', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 101) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Ijin', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 102) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Sakit', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 103) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Alpha', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                    } else {
                    jika();
                }
            /* Batas */

            }                
            if (nrp == 80111315) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String nama = "INDRA YULIATMA";
                if(codejob == 100) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Cuti(Tahunan, Site)', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 101) {
                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Ijin', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 102) {

                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Sakit', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                }
                if(codejob == 103) {

                    db.execSQL("insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', 'Alpha', '" + text3.getText().toString() + "', '" + text4.getText().toString() + "', '" + text5.getText().toString() + "', '" + text6.getText().toString() + "', '" + text7.getText().toString() + "', '" + text8.getText().toString() + "', '" + text9.getText().toString() + "', '" + text10.getText().toString() + "','" + id + "')");
                    codejob();
                } else {
                jika();
            }
        /* Batas */

        } 

我想让它更简单..你们可以帮助我简化吗?

当我尝试制作它时我遇到了很多麻烦...因为它总是让我看到db.execsql中的“nama”变量有问题

1 个答案:

答案 0 :(得分:0)

我看到insert语句都是相同的,除了中间的一个字符串。我首先要为相同的较大部分制作一个单独的字符串,然后为不同的部分添加一个参数:

String sqlStatementFormat = "insert into jan(no, instructor, branch, area, codejob, category, codecategory, activity, description, sapsomp, periodestart, periodeend, hrs, venue, vendor, unittype, remark, nomor) values('" + nrp + "','" + nama + "' ,'BLP',' TTA BALIKPAPAN','" + codejob + "', 'Absent', 'Absent', 'Absent', '%s', '" + text3.getText().toString() + "', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"

然后我会使用HashMap存储不同的" codeJob"每个整数的字符串:

HashMap<Integer, String> map = new HashMap<>();
map.put(100, "Cuti(Tahunan, Site)");
map.put(101, "etc");

等等。

然后,不是使用if语句来获取正确的文本,而是执行以下操作:

String codeString = map.get(codeJob);
String sql = String.format(sqlFormat, codeString, text3.getText().toString(), text4.getText().toString, text5.getText().toString(), text6.getText().toString(), text7.getText().toString(), text8.getText().toString(), text9.getText().toString(), text10.getText().toString(), id);

你可以用&#34; nrp&#34;做类似的事情。整数;没有理由你不能拥有另一张具有合法价值指数的地图。但这会使它更简单,并会给你这个想法。