这是正确的查询吗?
DbFertility info = new DbFertility(this);
info.open();
String name = info.getUserInfo();
info.close();
if(name != null){
Intent i = new Intent(this, Create.class);
startActivity(i);
}else{
Intent i2 = new Intent(this, CalendarMain.class);
startActivity(i2);
}
问题:如果名称为null我想指示它创建类..我真正的问题是我的名字是空的或不是它总是指示我到CalendarMain。(帮助)
public String getUserInfo() {
String[] columns = new String[]{KEY_USER_ID, KEY_NAME, KEY_AGE, KEY_STATUS};
Cursor c = getOurDatabase().query(TABLE_USER, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_USER_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iAge c.getColumnIndex(KEY_AGE);
int iStatus = c.getColumnIndex(KEY_STATUS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iName) + " " + c.getString(iAge) + c.getString(iStatus) + "\n";
}
return result;
}
保存:
public long createUser(String name, String age, String status) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_AGE, age);
cv.put(KEY_STATUS, status);
return getOurDatabase().insert(TABLE_USER, null, cv);
}
答案 0 :(得分:1)
使用TextUtils.isEmpty(name)
,如果字符串为null或者长度等于零,则返回true。
Intent i = null;
if(!TextUtils.isEmpty(name)) {
i = new Intent(this, Create.class);
} else {
i = new Intent(this, CalendarMain.class);
}
startActivity(i);