如何缩短这个java代码?

时间:2014-05-29 09:05:08

标签: java android

我正在为android创建一个时间表应用程序并加载一个JSON文件,其中包含要在TextView s表中解析的所有数据。

很多复制粘贴工作。现在我使用了很多相同的代码。 是否可以缩短此代码?如果是这样,怎么样?

        resetrooster();             
    String jsonStr = jsonStr(); 

    try {
        JSONObject jsonObj = new JSONObject(jsonStr);

        //JSON Array node
        removed_classes = jsonObj.getJSONArray(TAG_REMOVED_CLASSES);
        classes = jsonObj.getJSONArray(TAG_CLASSES);
        new_classes = jsonObj.getJSONArray(TAG_NEW_CLASSES);


        //Looping through all lessons
        for (int i = 0; i < classes.length(); i++) {
            JSONObject c = classes.getJSONObject(i);

            String day = c.getString(TAG_DAY);
            String room = c.getString(TAG_ROOM);
            String hour = c.getString(TAG_HOUR);
            String classs = c.getString(TAG_CLASSS);
            String teacher = c.getString(TAG_TEACHER);

            //Fill in the right lessons for the right day
            if (theday == 1){
                if (day.contains("1")){
                    if (hour.contains("1") && !hour.contains("10") && !hour.contains("11") && !hour.contains("12") && !hour.contains("13") ){
                        uur1.setText(hour); doc1.setText(teacher); vak1.setText(classs); lok1.setText(room);}
                        if (hour.contains("2")&& !hour.contains("12")){
                            uur2.setText(hour); doc2.setText(teacher); vak2.setText(classs); lok2.setText(room);}
                        if (hour.contains("3")&& !hour.contains("13")){
                            uur3.setText(hour); doc3.setText(teacher); vak3.setText(classs); lok3.setText(room);}
                        if (hour.contains("4")){
                            uur4.setText(hour); doc4.setText(teacher); vak4.setText(classs); lok4.setText(room);}
                        if (hour.contains("5")){
                            uur5.setText(hour); doc5.setText(teacher); vak5.setText(classs); lok5.setText(room);}
                        if (hour.contains("6")){
                            uur6.setText(hour); doc6.setText(teacher); vak6.setText(classs); lok6.setText(room);}
                        if (hour.contains("7")){
                            uur7.setText(hour); doc7.setText(teacher); vak7.setText(classs); lok7.setText(room);}
                        if (hour.contains("8")){
                            uur8.setText(hour); doc8.setText(teacher); vak8.setText(classs); lok8.setText(room);}
                        if (hour.contains("9")){
                            uur9.setText(hour); doc9.setText(teacher); vak9.setText(classs); lok9.setText(room);}
                        if (hour.contains("10")){
                            uur10.setText(hour); doc10.setText(teacher); vak10.setText(classs); lok10.setText(room);}
                        if (hour.contains("11")){
                            uur11.setText(hour); doc11.setText(teacher); vak11.setText(classs); lok11.setText(room);}
                        if (hour.contains("12")){
                            uur12.setText(hour); doc12.setText(teacher); vak12.setText(classs); lok12.setText(room);}
                        if (hour.contains("13")){
                            uur13.setText(hour); doc13.setText(teacher); vak13.setText(classs); lok13.setText(room);}

                    }}
            else if (theday == 2){
                if (day.contains("2")){
                        if (hour.contains("1") && !hour.contains("10") && !hour.contains("11") && !hour.contains("12") && !hour.contains("13") ){
                            uur1.setText(hour); doc1.setText(teacher); vak1.setText(classs); lok1.setText(room);}
                        if (hour.contains("2")&& !hour.contains("12")){
                            uur2.setText(hour); doc2.setText(teacher); vak2.setText(classs); lok2.setText(room);}
                        if (hour.contains("3")&& !hour.contains("13")){
                            uur3.setText(hour); doc3.setText(teacher); vak3.setText(classs); lok3.setText(room);}
                        if (hour.contains("4")){
                            uur4.setText(hour); doc4.setText(teacher); vak4.setText(classs); lok4.setText(room);}
                        if (hour.contains("5")){
                            uur5.setText(hour); doc5.setText(teacher); vak5.setText(classs); lok5.setText(room);}
                        if (hour.contains("6")){
                            uur6.setText(hour); doc6.setText(teacher); vak6.setText(classs); lok6.setText(room);}
                        if (hour.contains("7")){
                            uur7.setText(hour); doc7.setText(teacher); vak7.setText(classs); lok7.setText(room);}
                        if (hour.contains("8")){
                            uur8.setText(hour); doc8.setText(teacher); vak8.setText(classs); lok8.setText(room);}
                        if (hour.contains("9")){
                            uur9.setText(hour); doc9.setText(teacher); vak9.setText(classs); lok9.setText(room);}
                        if (hour.contains("10")){
                            uur10.setText(hour); doc10.setText(teacher); vak10.setText(classs); lok10.setText(room);}
                        if (hour.contains("11")){
                            uur11.setText(hour); doc11.setText(teacher); vak11.setText(classs); lok11.setText(room);}
                        if (hour.contains("12")){
                            uur12.setText(hour); doc12.setText(teacher); vak12.setText(classs); lok12.setText(room);}
                        if (hour.contains("13")){
                            uur13.setText(hour); doc13.setText(teacher); vak13.setText(classs); lok13.setText(room);}
                    }}
            else if (theday == 3){
                if (day.contains("3")){
                    if (hour.contains("1") && !hour.contains("10") && !hour.contains("11") && !hour.contains("12") && !hour.contains("13") ){
                        uur1.setText(hour); doc1.setText(teacher); vak1.setText(classs); lok1.setText(room);}
                        if (hour.contains("2")&& !hour.contains("12")){
                            uur2.setText(hour); doc2.setText(teacher); vak2.setText(classs); lok2.setText(room);}
                        if (hour.contains("3")&& !hour.contains("13")){
                            uur3.setText(hour); doc3.setText(teacher); vak3.setText(classs); lok3.setText(room);}
                        if (hour.contains("4")){
                            uur4.setText(hour); doc4.setText(teacher); vak4.setText(classs); lok4.setText(room);}
                        if (hour.contains("5")){
                            uur5.setText(hour); doc5.setText(teacher); vak5.setText(classs); lok5.setText(room);}
                        if (hour.contains("6")){
                            uur6.setText(hour); doc6.setText(teacher); vak6.setText(classs); lok6.setText(room);}
                        if (hour.contains("7")){
                            uur7.setText(hour); doc7.setText(teacher); vak7.setText(classs); lok7.setText(room);}
                        if (hour.contains("8")){
                            uur8.setText(hour); doc8.setText(teacher); vak8.setText(classs); lok8.setText(room);}
                        if (hour.contains("9")){
                            uur9.setText(hour); doc9.setText(teacher); vak9.setText(classs); lok9.setText(room);}
                        if (hour.contains("10")){
                            uur10.setText(hour); doc10.setText(teacher); vak10.setText(classs); lok10.setText(room);}
                        if (hour.contains("11")){
                            uur11.setText(hour); doc11.setText(teacher); vak11.setText(classs); lok11.setText(room);}
                        if (hour.contains("12")){
                            uur12.setText(hour); doc12.setText(teacher); vak12.setText(classs); lok12.setText(room);}
                        if (hour.contains("13")){
                            uur13.setText(hour); doc13.setText(teacher); vak13.setText(classs); lok13.setText(room);}
                    }}
            else if (theday == 4){
                if (day.contains("4")){
                    if (hour.contains("1") && !hour.contains("10") && !hour.contains("11") && !hour.contains("12") && !hour.contains("13") ){
                        uur1.setText(hour); doc1.setText(teacher); vak1.setText(classs); lok1.setText(room);}
                        if (hour.contains("2")&& !hour.contains("12")){
                            uur2.setText(hour); doc2.setText(teacher); vak2.setText(classs); lok2.setText(room);}
                        if (hour.contains("3")&& !hour.contains("13")){
                            uur3.setText(hour); doc3.setText(teacher); vak3.setText(classs); lok3.setText(room);}
                        if (hour.contains("4")){
                            uur4.setText(hour); doc4.setText(teacher); vak4.setText(classs); lok4.setText(room);}
                        if (hour.contains("5")){
                            uur5.setText(hour); doc5.setText(teacher); vak5.setText(classs); lok5.setText(room);}
                        if (hour.contains("6")){
                            uur6.setText(hour); doc6.setText(teacher); vak6.setText(classs); lok6.setText(room);}
                        if (hour.contains("7")){
                            uur7.setText(hour); doc7.setText(teacher); vak7.setText(classs); lok7.setText(room);}
                        if (hour.contains("8")){
                            uur8.setText(hour); doc8.setText(teacher); vak8.setText(classs); lok8.setText(room);}
                        if (hour.contains("9")){
                            uur9.setText(hour); doc9.setText(teacher); vak9.setText(classs); lok9.setText(room);}
                        if (hour.contains("10")){
                            uur10.setText(hour); doc10.setText(teacher); vak10.setText(classs); lok10.setText(room);}
                        if (hour.contains("11")){
                            uur11.setText(hour); doc11.setText(teacher); vak11.setText(classs); lok11.setText(room);}
                        if (hour.contains("12")){
                            uur12.setText(hour); doc12.setText(teacher); vak12.setText(classs); lok12.setText(room);}
                        if (hour.contains("13")){
                            uur13.setText(hour); doc13.setText(teacher); vak13.setText(classs); lok13.setText(room);}
                    }}
            else if (theday == 5){
                if (day.contains("5")){
                    if (hour.contains("1") && !hour.contains("10") && !hour.contains("11") && !hour.contains("12") && !hour.contains("13") ){
                        uur1.setText(hour); doc1.setText(teacher); vak1.setText(classs); lok1.setText(room);}
                        if (hour.contains("2")&& !hour.contains("12")){
                            uur2.setText(hour); doc2.setText(teacher); vak2.setText(classs); lok2.setText(room);}
                        if (hour.contains("3")&& !hour.contains("13")){
                            uur3.setText(hour); doc3.setText(teacher); vak3.setText(classs); lok3.setText(room);}
                        if (hour.contains("4")){
                            uur4.setText(hour); doc4.setText(teacher); vak4.setText(classs); lok4.setText(room);}
                        if (hour.contains("5")){
                            uur5.setText(hour); doc5.setText(teacher); vak5.setText(classs); lok5.setText(room);}
                        if (hour.contains("6")){
                            uur6.setText(hour); doc6.setText(teacher); vak6.setText(classs); lok6.setText(room);}
                        if (hour.contains("7")){
                            uur7.setText(hour); doc7.setText(teacher); vak7.setText(classs); lok7.setText(room);}
                        if (hour.contains("8")){
                            uur8.setText(hour); doc8.setText(teacher); vak8.setText(classs); lok8.setText(room);}
                        if (hour.contains("9")){
                            uur9.setText(hour); doc9.setText(teacher); vak9.setText(classs); lok9.setText(room);}
                        if (hour.contains("10")){
                            uur10.setText(hour); doc10.setText(teacher); vak10.setText(classs); lok10.setText(room);}
                        if (hour.contains("11")){
                            uur11.setText(hour); doc11.setText(teacher); vak11.setText(classs); lok11.setText(room);}
                        if (hour.contains("12")){
                            uur12.setText(hour); doc12.setText(teacher); vak12.setText(classs); lok12.setText(room);}
                        if (hour.contains("13")){
                            uur13.setText(hour); doc13.setText(teacher); vak13.setText(classs); lok13.setText(room);}
                    }}

        }

    } catch (JSONException e) {
        e.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

我首先要创建一个类&#34; Session&#34;它将uurN,docN,vakN和lokN字段封装为实例变量(也更好地命名)。然后添加一个fill方法来接受这四个字段并填写实例变量。

public class Session {
    private final TextView hour;
    private final TextView teacher;
    private final TextView subject;
    private final TextView room;

    public Session(TextView hourTextView, TextView teacherTextView, TextView subjectTextView, TextView roomTextView) {
        this.hourTextView = hourTextView;
        this.teacherTextView = teacherTextView;
        this.subjectTextView = subjectTextView;
        this.roomTextView = roomTextView;
    }

    public void fill(String hour, String teacher, String subject, String room) {
        hourTextView.setText(hour);
        teacherTextView.setText(teacher);
        subjectTextView.setText(subject);
        roomTextView.setText(room);
    }
}

然后我会创建一个HashMap,让它调用会话,将小时映射到会话对象。

HashMap<Byte, Session> sessions = new HashMap<Byte, Session>();
sessions.add(1, new Session(uur1, doc1, vak1, lok1));
sessions.add(2, new Session(uur2, doc2, vak2, lok2)); //etc

然后你可以做类似的事情:

sessions.get(hour).fill(hour, teacher, classs, room);

如果你需要测试相等性而不是someString.contains(&#34; 1&#34;),那么只需使用someString.equals(&#34; 1&#34;)然后需要排除所有其他可能性。