0

我正在尝试使用 else if 语句更改我的应用程序中相同 String 属性中的值,但到目前为止一直面临一些问题。直到现在我还没有能够改变价值观。这是我的代码的重要部分。有什么建议么?

protected void ScheduleOp1() {
   // TODO Auto-generated method stub
   String VarMin = "-140 minute";

   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()) {
      Toast.makeText(
            getBaseContext(),
            cur.getString(0) + "  " + cur.getString(1) + "  "
                  + cur.getString(2) + "  " + cur.getString(3) + "\n",
            Toast.LENGTH_LONG).show();
   } else if (cur.moveToFirst() == false) {
      VarMin = "-150 minute";
   } else {
      Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show();
   }
}

希望你们能帮帮我。

- 更新 -

这是代码的更新。我正在尝试做的是不断更改“VarMin”的值,以便通过我创建的视图执行搜索。'else' 语句适用于绝对没有找到结果的情况。

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()) {




                Toast.makeText(getBaseContext(), cur.getString(0)+ "  " +cur.getString(1)+ "  "
                 +cur.getString(2)+ "  " +cur.getString(3)+ "\n",
                    Toast.LENGTH_LONG).show();
            }else if (cur.moveToFirst() == false){
                VarMin = "-145 minute"; 
            }
            else if (cur.moveToFirst() == false){
                VarMin = "-150 minute";
            }
            else{
                Toast.makeText(this, "Not found", 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);
           }

    }

希望你们能帮助我。

- 更新 -

这是完整代码的更新。

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=140;
    private String getVarMin(){
        return "-"+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


            Cursor cur = null;
            boolean mustCont = true;
            while (mustCont){
                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+ "', '" +getVarMin()+"');");
                cur = fetchAllTodos();

                if (cur.getCount() == 0){
                    switch (Min){
                    case 140 :
                        Min = 145;
                        break;
                    case 145 :
                        Min = 150;
                        break;
                    case 150 :
                        Min = 155;
                        break;
                    case 155 :
                        Min = 180;
                        break;
                    case 200 :
                        mustCont = false;
                        break;
                    }

                }else{
                    mustCont = false;
                }
            }
            if(cur != null && cur.getCount()>0){
                cur.moveToFirst();
                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


            Cursor cur = null;
            boolean mustCont = true;
            while (mustCont){
                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+ "', '" +getVarMin()+"');");
                cur = fetchAllTodos();

                if (cur.getCount() == 0){
                    switch (Min){
                    case 140 :
                        Min = 145;
                        break;
                    case 145 :
                        Min = 150;
                        break;
                    case 150 :
                        Min = 155;
                        break;
                    case 155 :
                        Min = 180;
                        break;
                    case 200 :
                        mustCont = false;
                        break;
                    }

                }else{
                    mustCont = false;
                }
            }
            if(cur != null && cur.getCount()>0){
                cur.moveToFirst();
                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();
                }
    }

希望你们能帮帮我。谢谢

4

3 回答 3

0

您是否尝试过使用全局变量private String VarMin = "-140 minute";

于 2012-05-03T12:03:33.640 回答
0

if没有任何意义...... moveToFirst 为真,或者为假(意味着没有返回任何记录)。基本上,您现在拥有的将永远不会显示“未找到”吐司。

因此,不清楚您是想在没有记录时显示吐司,还是设置变量。

无论哪种方式,你都应该清理你if的东西(假设你想要变量集):

if (cur.moveToFirst()) {
  Toast.makeText(
     getBaseContext(),
     cur.getString(0) + "  " + cur.getString(1) + "  "
     + cur.getString(2) + "  " + cur.getString(3) + "\n",
     Toast.LENGTH_LONG).show();
  } else {
     VarMin = "-150 minute";
  } 

编辑

并且您的代码的排列方式,您总是将“-140 分钟”放入您的数据库中。如果您希望 IF 所做的更改进入数据库,则需要在 db 操作之前将其移动。

于 2012-05-03T12:33:58.457 回答
0

编辑:我添加了管理查询的代码

你可以做的事情:

private int mMinutes = 145;
protected void ScheduleOp1() {
    Cursor cur = null;
    boolean mustContinue = true  ;
    while(mustContinue){
         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+ "', '-" +mMinutes+" minute');");
      cur = fetchAllTodos()
        if(cur.getCount()==0){
            mustContinue = true ;
            switch(mMinutes){
            case 145 :
                mMinutes = 150 ;
                break;
            case 150 :
                mMinutes = 155 ;
                break;
            case 200 : //for max value for example
                mustContinue = false ;
                break;
                //etc ....
            }

        }else{
            mustContinue = false ;
        }
    }
    if(cur != null && cur.getCount()>0){
        cur.moveToFirst();
        Toast.makeText(getBaseContext(), cur.getString(0)+ "  " +cur.getString(1)+ "  "
                 +cur.getString(2)+ "  " +cur.getString(3)+ "\n",
                    Toast.LENGTH_LONG).show();
    }else{
        mMinutes = 145;
        Toast.makeText(this, "Not found", Toast.LENGTH_LONG).show();
    }
}
于 2012-05-03T13:43:38.397 回答