问题是数据库查询存在一些问题。我首先检查它是否存在于数据库中。如果它存在,那么我只是从数据库中取出后启动一个意图。如果它不存在则从web获取并放入数据库
这就是我称之为数据库功能的地方
public static void main(String[] args){
method();
boolean x = true;
if(x == y()){
System.out.println("You're right!");
}else{
System.out.println("You're wrong!");
main(args);
}
}
public static void method(){
System.out.println("a+b=5 If a=2 what does b equal?");
}
public static boolean y(){
try{
Scanner s = new Scanner(System.in);
String input = s.nextLine();
int input2 = Integer.parseInt(input);
int b = 3;
return input2 == b;
}
catch( Exception e ) {
return false;
}
}
}
这是我的databaseChecker方法
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
urlBlogtitle = mainBlogPost.get(position).title; // getting title of the clicked item
Log.d(TAG, urlBlogtitle);
summarygetter=databaseChecker(urlBlogtitle);
if(summarygetter=="") new DownloadXmlTaskContent(MainListActivity.this).execute(URL);
else startingIntent(summarygetter);
// intitating the main object for Content parsing
}
这是我的数据库方法
private String databaseChecker(String title) {
DatabaseHandler db = new DatabaseHandler(MainListActivity.this);
String content=" ";
content = db.getContact(title);
Log.d(TAG,content);
return content;
}
这是错误的logcat视图。
String getContact(String title) {
SQLiteDatabase db = this.getReadableDatabase();
String content = " ";
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_TITLE, KEY_CONTENT }, KEY_ID + "=?",
new String[] { title }, null, null, null, null);
if (cursor != null){
cursor.moveToFirst();}
else
{
return content;
}
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
content=contact.title;
// return contact
return content;
}
答案 0 :(得分:0)
问题是你没有检查cursor.moveToFirst()
的返回值,如果没有条目,它将返回false。而不是:
if (cursor != null){
cursor.moveToFirst();}
else
{
return content;
}
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
content=contact.title;
// return contact
return content;
试试这个:
if (cursor.moveToFirst()){
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
content=contact.title;
// return contact
return content;
} else{
return "Whatever_you_return_when_not_found"
}