参考:https://www.xuebuyuan.com/3244495.html
原来错误的程序,app会崩溃(请大神指点错误的原因是啥?_(:з)∠)_)
1 btn_search.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View v) { 4 String key = et_username.getText().toString(); 5 Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_user",null,"username=?",new String[]{key}, null,null,null); 6 7 if (cursor.getCount() == 0) { 8 Toast.makeText(MainActivity.this,"很遗憾,没有相关记录",Toast.LENGTH_SHORT).show(); 9 } else { 10 // while (cursor.moveToNext()) { 11 // Log.i("cccccccccc", cursor.getString(1) " " cursor.getString(2)); 12 // } 13 Log.i("dddddddd",cursor.getString(1)); 14 } 15 } 16 });
改正后的程序,用循环来输出
1 btn_search.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View v) { 4 String key = et_username.getText().toString(); 5 Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_user",null,"username=?",new String[]{key}, null,null,null); 6 7 if (cursor.getCount() == 0) { 8 Toast.makeText(MainActivity.this,"很遗憾,没有相关记录",Toast.LENGTH_SHORT).show(); 9 } else { 10 while (cursor.moveToNext()) { 11 Log.i("cccccccccc", cursor.getString(1) " " cursor.getString(2)); 12 } 13 // Log.i("dddddddd",cursor.getString(1)); 14 } 15 } 16 });