0

我目前正在尝试在我的 android 应用程序的 if else 语句中更改 int 值。到目前为止,我已经尝试了很多方法并在互联网上进行了搜索,但找不到任何解决方案。

这是我的代码。

package sirtat.individualProject.PublicTransportationTimeScheduler;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;

import android.widget.Toast;

import android.widget.TimePicker;
import android.widget.DatePicker;

import java.util.Calendar;
import java.util.Locale;
import android.database.Cursor;

public class ScheduleActivity extends Activity {
    SQLiteDatabase db;

    TimePicker timePicker;
    DatePicker datePicker;


    int hour, minute;
    int yr, month, day;


    static String fDate1="DepartureDate";
    static String fDate2="DepartureDate";
    static String fTime="DEPART_TIME";
    static String fTime2="DEPART_TIME";

    public static final String view_Schedule1 = "ViewSchedule1";
    public static final String sBusArrival = "SBusArrivalLocation";
    public static final String sBusDepart = "SBusDepartLocation";
    public static final String sBusArrivalT = "SBusArrivalTime";
    public static final String sBusDepartT = "SBusDepartTime";
    public static final String fDepart = "FlightDeparture";
    public static final String sBusSchedule = "SkyBusSchedule";
    public static final String sBusDate = "SkyBusDate";
    public static final String fDate = "FlightDate";

    private int Min;
    private String VarMin = Min+" " + "minute";


    //private String VarMin = "";

    /** Called when the activity is first created */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_layout);

        db = openOrCreateDatabase("PublicTransport2" , SQLiteDatabase.CREATE_IF_NECESSARY, null);

        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true); 



        Calendar today = Calendar.getInstance();

        //--GET THE CURRENT DATE--
        yr= today.get(Calendar.YEAR);
        month = today.get(Calendar.MONTH);
        day = today.get(Calendar.DAY_OF_MONTH);

        //--GET THE CURRENT TIME--
        hour = today.get(Calendar.HOUR);
        minute = today.get(Calendar.MINUTE);

        timePicker = (TimePicker) findViewById(R.id.timePicker);

        datePicker = (DatePicker) findViewById(R.id.datePicker);





        //--Button view--
                Button btnOp1 = (Button) findViewById(R.id.btnOption1);
                btnOp1.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        int year1 = datePicker.getYear();
                        int mnth1 = datePicker.getMonth() + 1;
                        int dy1 = datePicker.getDayOfMonth();
                        int hr1 = timePicker.getCurrentHour();
                        int min1 = timePicker.getCurrentMinute();

                        fDate1 = (pad(year1)) + ("-") + (pad(mnth1))
                             + ("-") + (pad(dy1));
                        fTime = (pad(hr1)) + (":") + (pad(min1));

                        ScheduleOp1();  
                            }
                    private Object pad(int b) {
                        // TODO Auto-generated method stub
                        if (b >= 10){
                            return String.valueOf(b);
                        }else{
                           return "0" + String.valueOf(b);
                        }


                    }
                   });  

                Button btnOp2 = (Button) findViewById(R.id.btnScheduleOption2);
                btnOp2.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        int year2 = datePicker.getYear();
                        int mnth2 = datePicker.getMonth() + 1;
                        int dy2 = datePicker.getDayOfMonth();
                        int hr2 = timePicker.getCurrentHour();
                        int min2 = timePicker.getCurrentMinute();

                        fDate2 = (pad2(year2)) + ("-") + (pad2(mnth2))
                             + ("-") + (pad2(dy2));
                            fTime2 = (pad2(hr2)) + (":") + (pad2(min2));

                            ScheduleOp2();



                    }


                    private Object pad2(int c) {
                        // TODO Auto-generated method stub
                        if (c >= 10){
                            return String.valueOf(c);
                        }else{
                           return "0" + String.valueOf(c);
                        }
                    }

                });

            }

    protected void ScheduleOp1() {
        // TODO Auto-generated method stub


        db.execSQL("DROP VIEW IF EXISTS " +view_Schedule1);
        db.execSQL("CREATE VIEW " +view_Schedule1+ " " +
                " AS SELECT " +sBusDepart+ "," +sBusArrival+ "," 
                +sBusDepartT+ "," +sBusArrivalT+ " FROM" + " "
                +fDepart+ " " + "INNER JOIN" + " " +sBusSchedule+ " " +
                "ON " +sBusSchedule+ "." +sBusDate+ "=" +fDepart+ "." 
                +fDate+ " " + "WHERE " +fDate+ "= '" +fDate1+"'"+" "+
                "AND" + " " +sBusDepartT+ " " + "= strftime('%H:%M', '" +fTime+ "', '" +VarMin+"');");

         Cursor cur = fetchAllTodos();
            startManagingCursor (cur);


        if (cur.moveToFirst()) {

            for(int Min = -140; Min >= -150; Min=Min-5)
            {
            Toast.makeText(getBaseContext(), cur.getString(0)+ "  " +cur.getString(1)+ "  "
                     +cur.getString(2)+ "  " +cur.getString(3)+ "\n",
                        Toast.LENGTH_LONG).show();
            }
        }else {
            Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show();
        }
            }     





    protected void ScheduleOp2(){

        Toast.makeText(getBaseContext(), "Date selected:"
                 + fDate2 + "\n" +
                "Time Selected:" + fTime2,
                Toast.LENGTH_LONG).show();
    }


           //fetching records from database
         public  Cursor fetchAllTodos() {   
         return db.query(view_Schedule1, new String [] {sBusDepart, 
                 sBusArrival, sBusDepartT, sBusArrivalT }, null, null, null, null, null);
           }



    }

这是代码的重要部分

protected void ScheduleOp1() {
        // TODO Auto-generated method stub


        db.execSQL("DROP VIEW IF EXISTS " +view_Schedule1);
        db.execSQL("CREATE VIEW " +view_Schedule1+ " " +
                " AS SELECT " +sBusDepart+ "," +sBusArrival+ "," 
                +sBusDepartT+ "," +sBusArrivalT+ " FROM" + " "
                +fDepart+ " " + "INNER JOIN" + " " +sBusSchedule+ " " +
                "ON " +sBusSchedule+ "." +sBusDate+ "=" +fDepart+ "." 
                +fDate+ " " + "WHERE " +fDate+ "= '" +fDate1+"'"+" "+
                "AND" + " " +sBusDepartT+ " " + "= strftime('%H:%M', '" +fTime+ "', '" +VarMin+"');");

         Cursor cur = fetchAllTodos();
            startManagingCursor (cur);


        if (cur.moveToFirst()) {

            for(int Min = -140; Min >= -150; Min=Min-5)
            {
            Toast.makeText(getBaseContext(), cur.getString(0)+ "  " +cur.getString(1)+ "  "
                     +cur.getString(2)+ "  " +cur.getString(3)+ "\n",
                        Toast.LENGTH_LONG).show();
            }
        }else {
            Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show();
        }
            }

我尝试使用 for 循环来减少值,但到目前为止还没有让它工作。有什么建议或更好的解决方案吗?

4

1 回答 1

2

局部变量Min

for(int Min = -140; Min >= -150; Min=Min-5)

与实例成员不同Min

private int Min;

如果要更改实例成员的值,则需要将 for 循环更改为:

for(Min = -140; Min >= -150; Min=Min-5)

顺便说一句,你也可以写:

for(Min = -140; Min >= -150; Min-=5)

另请注意,更改 的值Min不会VarMin自动更改 的值。如果您希望它们被链接,则VarMin每次更新时都需要更新。Min

最后,另一方面,Java 中的约定是对变量使用小型大写字母:Min=> minVarMin=>varMin等。

编辑

与其创建varMin成员,不如创建一个私有方法:

private String getVarMin() {
    return min+" " + "minute";
}

并在您使用该varMin变量的任何地方调用该方法。这样,您可以确保始终使用最新的 min 值。

于 2012-05-04T11:47:23.477 回答