In My App, user creates a new account by filling some credentials in a dialog box. There is a GridView
inside dialog box which different different icons for accounts. User can select any icon for his account while creating the account. The problem is Image is not showing in list view and in DB browser for SQLite too. I don't know what should I do even after too much searching. Here is my try :
Int array containing images : (Many images are in .svg format while others are in .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};
After Button clicked :
@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();
}
}
});
Methods : 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();
}
In getView()
of my custom 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());
DatabaseHelper class :
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;
}
Please I need some help !