gpt4 book ai didi

android - EditText afterTextChanged 不工作 : displays null

转载 作者:IT王子 更新时间:2023-10-29 06:24:07 26 4
gpt4 key购买 nike

我使用以下代码将 Uri 图像插入到 SQLite 中:

注册

private void insertData( String name,String pass, Uri image) throws SQLiteException {
database = mdb.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(MyDatabaseHelper.KEY_NAME, name);
cv.put(MyDatabaseHelper.KEY_PASSWORD, pass);
try {
database = mdb.getWritableDatabase();
InputStream iStream = getContentResolver().openInputStream(image);
byte[] inputData = Utils.getBytes(iStream);
cv.put(MyDatabaseHelper.KEY_IMAGE,inputData);
}catch(IOException ioe)
{
Log.e(TAG, "<saveImageInDB> Error : " + ioe.getLocalizedMessage());
}
database.insert(MyDatabaseHelper.TABLE_USER, null, cv);
Toast.makeText(getApplicationContext(),"Database Created",Toast.LENGTH_SHORT).show();
database.close();
}

当我看到 Database Created 时,我假设数据库已成功创建并且数据已插入。

Login 中,我希望根据用户名从 SQLite 检索图像。

name = (EditText) findViewById(R.id.name);

name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable editable) {
String personName = name.getText().toString();
database = mdb.getWritableDatabase();
String selectQuery = " SELECT " + MyDatabaseHelper.KEY_IMAGE + " FROM " + MyDatabaseHelper.TABLE_USER + " WHERE " + MyDatabaseHelper.KEY_NAME + " = ' " + personName + " ' ";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
byte[] blob = cursor.getBlob(cursor.getColumnIndex("Image"));
Log.e("A", blob+"");
cursor.close();
mdb.close();
imageView.setImageBitmap(getRoundedBitmap(Utils.getImage(blob)));
}
else
{
Toast.makeText(getApplication(),"NULL",Toast.LENGTH_SHORT).show();
}
}
});

public Bitmap getRoundedBitmap(Bitmap bitmap){
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
return circleBitmap;
}

我的数据库助手

public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="mm.db";
public static final String TABLE_USER="User";
public static final String KEY_NAME="Name";
public static final String KEY_PASSWORD="Password";
public static final String KEY_IMAGE="Image";
public static final String ID="id";

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_USER + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Password TEXT,Image BLOB )");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version" + oldVersion + "to" + newVersion + ",which will destroy all old data");
db.execSQL("Drop TABLE IF EXISTS " + TABLE_USER);
onCreate(db);
}

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

工具

public class Utils {

public static byte[] getImageBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}

public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}

public static byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
}

用户在 EditText 上输入他/她的名字后,名为 name 的图像没有检索,但向我显示 Null 消息!这里出了什么问题?

最佳答案

对于 EditText,每次 TextWatcher() 检测到文本更改时,afterTextChanged() 都会被调用,即在 EditText 中键入的每个字符都是一个textChange 事件。

因此,即使在键入整个用户名之前,afterTextChanged() 也会被多次调用,因此会出现空游标。但是,一旦用户名完全键入,光标就会提供正确的行,在您的情况下,是图像 blob。

正如您提到的,它是一个登录页面,因此必须有一个密码 editText 以及用户名 editText。

我建议的更好方法是使用 setOnFocusChangeListener() 检测用户名 EditText 的焦点变化。用户完成输入用户名后立即触发 Sql 查询,焦点现在位于密码 editText 上。

name = (EditText) findViewById(R.id.name);

name.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) { //Fire Query when focus is lost
String personName = name.getText().toString();
database = mdb.getWritableDatabase();
String selectQuery = " SELECT " + MyDatabaseHelper.KEY_IMAGE + " FROM " + MyDatabaseHelper.TABLE_USER + " WHERE " + MyDatabaseHelper.KEY_NAME + " = ' " + personName + " ' ";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
byte[] blob = cursor.getBlob(cursor.getColumnIndex("Image"));
Log.e("A", blob+"");
cursor.close();
mdb.close();
imageView.setImageBitmap(getRoundedBitmap(Utils.getImage(blob)));
}
else
{
Toast.makeText(getApplication(),"NULL",Toast.LENGTH_SHORT).show();
}
}
});

让我们知道这是否有效。

关于android - EditText afterTextChanged 不工作 : displays null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com