android – EditText afterTextChanged不起作用

前端之家收集整理的这篇文章主要介绍了android – EditText afterTextChanged不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用下面的代码将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 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;
   }

MyDatabaseHelper

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,1);
    }
}

utils的

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,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,len);
        }
        return byteBuffer.toByteArray();
    }
}

用户在EditText上键入他/她的名字,名称为name,图片没有检索,但是向我显示Null消息!这有什么不对?

@R_404_323@

对于EditText,每当TextWatcher()检测到文本更改时,将调用afterTextChanged(),即EditText中键入的每个字符都是textChange事件.

因此,即使在输入整个用户名之前,也会多次调用afterTextChanged(),从而调用null游标.但是,只要用户名被完全输入,光标就应该提供正确的行,在您的情况下,是图像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(),Toast.LENGTH_SHORT).show();
            }
        }
});

如果有效,请告诉我们.

原文链接:https://www.f2er.com/android/314673.html

猜你在找的Android相关文章