2016-09-30 66 views
0

我有一個活動來檢查設備ID和設備名稱是否存在於本地數據庫(sqlite)中,當設備已經註冊時,我將傳遞給另一個名爲ConnectToCostCenter的活動 一個活動的代碼是:在sqlite數據庫中檢查設備ID和名稱

public class StartActivity extends Activity { 

Handler handler = new Handler(); 
DBHandler dbHandler; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.start_activity); 
    dbHandler = new DBHandler(this); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      String DeviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); 
      String DeviceName = Build.MODEL; 
      RegisterDevice cc = new RegisterDevice(DeviceId, DeviceName); 
      if (!dbHandler.getAllDevice().isEmpty() && (dbHandler.getAllDevice().contains(cc))){ 
       final Intent mainIntent = new Intent(StartActivity.this, ConnectToCostCenter.class); 
       startActivity(mainIntent); 
      } else { 
       Intent intent = new Intent(StartActivity.this, CreateOrEditActivity.class); 
       startActivity(intent); 
      } 
      StartActivity.this.finish(); 
     } 
    }, 3000); 
} 

}

和該ArrayList:

public ArrayList<RegisterDevice> getAllDevice() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    ArrayList<RegisterDevice> activityList = null; 
    try{ 
     activityList = new ArrayList<RegisterDevice>(); 
     String QUERY = "SELECT * FROM "+DEVICE_TABLE_NAME; 
     Cursor cursor = db.rawQuery(QUERY, null); 
     if(!cursor.isLast()) 
     { 
      while (cursor.moveToNext()) 
      { 
       RegisterDevice activity = new RegisterDevice(); 

       activity.setDeviceId(cursor.getString(0)); 
       //  activity.setCreated(cursor.getString(2)); 
       // activity.setModified(cursor.getString(3)); 
       activity.setDeviceName(cursor.getString(1)); 
       activityList.add(activity); 
      } 
     } 
     db.close(); 
    }catch (Exception e){ 
     Log.e("error",e+""); 
    } 
    return activityList; 
} 

問題是如果設備存在於數據庫中,那麼出現CreateOrEditActivity活動而不是ConnectToCostCenter 有什麼幫助?

+0

調試你的代碼並檢查你的數據庫是否包含你想要檢查的行或不 – Anjali

回答

0

我認爲你對匹配條件有點混淆,做一些另外一種方式就像迭代數組列表並在循環中創建條件,無論你是否可以得到真或假,並將活動重定向到你想要的地方。