首页 > 解决方案 > 如何通过单击网格视图项获取图像,然后将其保存在 SQLite 数据库中

问题描述

在我的应用程序中,用户通过在对话框中填写一些凭据来创建一个新帐户。有一个GridView内部对话框,其中包含不同的帐户图标。用户可以在创建帐户时为其帐户选择任何图标。问题是图像也没有显示在列表视图和 SQLite 的数据库浏览器中。即使经过太多搜索,我也不知道该怎么办。这是我的尝试:

包含图像的 Int 数组:(许多图像为 .svg 格式,而其他图像为 .png)

int[] icons = {R.drawable.personalbusaccount, R.drawable.ic_accountant, R.drawable.ic_bank_account,
        R.drawable.multipleaccount, R.drawable.ic_accounting, R.drawable.ic_consultant, R.drawable.choice,
        R.drawable.ic_businesswoman, R.drawable.ic_man, R.drawable.ic_login};

按钮点击后:

 @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(AccountsActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View v = inflater.inflate(R.layout.new_account_dialog, null);
            final EditText editText = v.findViewById(R.id.et_accname);

            final GridView gv = v.findViewById(R.id.accicon_grid_view);
            NewAccountGAdapter newAccountGAdapter = new NewAccountGAdapter(AccountsActivity.this, icons);
            gv.setAdapter(newAccountGAdapter);

            gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < gv.getChildCount(); i++) {
                        if(position == i ){
                            gv.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.colorLightGrey));
                            accountIcon = inttoByteArray(icons[position]);
                        }else{
                            gv.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                        }
                    }
                }
            });

            builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    accountName = editText.getText().toString();

                    boolean dataAdded = databaseHelper.addAccount(accountIcon, accountName, "0 Rs");
                    if (dataAdded){
                        Toast.makeText(AccountsActivity.this, "Account Created !", Toast.LENGTH_SHORT).show();
                        populateListView();
                    }else {
                        Toast.makeText(AccountsActivity.this, "Account not Created ", Toast.LENGTH_SHORT).show();
                    }

                }
            });

方法: inttoByteArray():

private byte[] inttoByteArray(final int i){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(bos);
    try {
        dataOutputStream.writeInt(i);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        dataOutputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bos.toByteArray();
}

getView()我的习惯中BaseAdapter

View view = LayoutInflater.from(context).inflate(R.layout.accounts_list_items, parent, false);
    ImageView imageView = view.findViewById(R.id.acc_icon);
    TextView textViewName = view.findViewById(R.id.tv_accName);
    TextView textViewBlnc = view.findViewById(R.id.tv_accBlnc);

    byte[] icons = accountsModelList.get(position).getIcon();

    Bitmap bm = BitmapFactory.decodeByteArray(icons, 0, icons.length);
    imageView.setImageBitmap(bm);
    textViewName.setText(accountsModelList.get(position).getAccName());
    textViewBlnc.setText(accountsModelList.get(position).getAccBlnc());

数据库助手类:

private static final String TABLE_NAME = "accounts";
private static final String TABLE_COL1 = "acc_id";
private static final String TABLE_COL2 = "acc_icon";
private static final String TABLE_COL3 = "acc_name";
private static final String TABLE_COL4 = "acc_blnc";

 public void onCreate(SQLiteDatabase db) {
    String accountsTable = "CREATE TABLE " + TABLE_NAME + " (" + TABLE_COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            TABLE_COL2 + " BLOB, " + TABLE_COL3 + " VARCHAR(300), " + TABLE_COL4 + " VARCHAR(500))";

db.execSQL(accountsTable);
}

public boolean addAccount(byte[] icon, String acName, String acBlnc){
    SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(TABLE_COL2, icon);
        contentValues.put(TABLE_COL3, acName);
        contentValues.put(TABLE_COL4, acBlnc);
        return db.insert(TABLE_NAME, null, contentValues)>0;
}

请我需要一些帮助!

标签: androidandroid-sqliteandroid-gridviewandroid-bitmapbytearrayinputstream

解决方案


推荐阅读