AlertDialog未显示有关获取单个数据Android数据库的数据

时间:2016-07-17 20:37:48

标签: android android-sqlite

在数据库中有1个表,其中包含字段'ID','NAME','ISSN','IMPACTFACTOR'。在UI中我有ADD按钮手动添加数据,LIST按钮查看所有数据和搜索按钮,以便我可以使用输入名称进行搜索。当我在Textfield输入名称并输入搜索时,AlertDialog Box没有结果,我在logcat上输出:android.database.sqlite.SQLiteCursor@2e6cefb

按名称搜索的代码

public class NameSearch extends Activity {

    DatabaseHelper myDb;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.name_activity);
        myDb  = new DatabaseHelper(this);
    }

    public void onNameSearch(View view) {
        Intent i = new Intent(this, SearchResult.class);

        final EditText name = (EditText) findViewById(R.id.nameInput);
        String nameSearch = name.getText().toString();
        Cursor cursor = myDb.getNameSearchData(nameSearch);
        //to see what values does cursor get
        String valueReturned = cursor.toString();
        Log.d("ADebugTag", "Value: " + valueReturned);
        if (cursor != null && cursor.getCount() > 0) {
            Toast.makeText(this, "Nothing Found", Toast.LENGTH_LONG).show();
            return;
        }
        StringBuffer buffer = new StringBuffer();
        while(cursor.moveToNext()){
            buffer.append("ID:" + cursor.getString(0)+ "\n");
            buffer.append("NAME:").append(cursor.getString(1)).append("\n");
            buffer.append("ISSN:" + cursor.getString(2)+ "\n");
            buffer.append("IMPACTfACTOR:").append(cursor.getString(3)).append("\n\n");
        }
            //i.putExtra("newMessage", finalValue);
        showMessage("Data Found", buffer.toString());
           // startActivity(i);
        }

    public void showMessage(String title , String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }
    }        

DataBaseHelper的代码

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "journal.db";
    public static final String TABLE_NAME = "journal";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "ISSN";
    public static final String COL_4 = "IMPACTFACTOR";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(" CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT , ISSN TEXT , IMPACTFACTOR REAL) " );
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS"  + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    public boolean insertData(String name, String issn , String impactfactor){
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,issn);
        contentValues.put(COL_4,impactfactor);
        long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
        return result != -1;

    }
    public Cursor getAllData(){
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        return sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    }

    public Cursor getNameSearchData(String name){
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor res1 = sqLiteDatabase.rawQuery("SELECT * FROM "+ TABLE_NAME +" WHERE NAME = '"+ name +"'",null);

          //to get  the result of res1
        String valueReturned = res1.toString();
        Log.d("ADebugTag", "Value: " + valueReturned);

        return res1;

    }

}

请提供解决方案

1.MobileScreenshot 2.LogcatTrace

1 个答案:

答案 0 :(得分:0)

if (cursor != null && cursor.getCount() > 0) {
    Toast.makeText(this, "Nothing Found", Toast.LENGTH_LONG).show();             
    return;
}

您可能希望“找不到任何内容”的条件为cursor.getCount() == 0而不是> 0

  

我在logcat上输出:android.database.sqlite.SQLiteCursor@2e6cefb

是的,这是您在toString()上致电Cursor时获得的结果。如果您想记录Cursor以进行调试,请查看DatabaseUtils#dumpCursor()