I have an app which reads notification from particular apps and insert in sqlite database and also contain listview and adapter which populate data from sqlite database into listview. Problem is that suppose if one notification that does not contain image and second notification contain then second one notification image is inserted in first notification which does not contain image. Pls help me to resolve.
code of database:-
private static final String DATABASE_NAME = "Notifications.db";
private static final int DATABASE_VERSION = 1;
private static final String NOTIFICATION_TABLE_NAME = "notifications";
private static final String NOTIFICATION_COLUMN_ID = "_id";
static final String NOTIFICATION_TITLE_COLUMN = "title";
static final String NOTIFICATION_PACKAGE_COLUMN = "package";
static final String NOTIFICATION_CONTENT_COLUMN = "text";
static final String NOTIFICATION_IMAGE = "image";
public NotificationDataBase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE " + NOTIFICATION_TABLE_NAME +
"(" + NOTIFICATION_COLUMN_ID + " INTEGER PRIMARY KEY, " +
NOTIFICATION_TITLE_COLUMN + " TEXT, " + NOTIFICATION_PACKAGE_COLUMN+" TEXT, "+NOTIFICATION_CONTENT_COLUMN + " TEXT, "+NOTIFICATION_IMAGE+" BLOB)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + NOTIFICATION_TABLE_NAME);
onCreate(db);
}
/*Method to insert notification into database*/
public boolean insertNotification(String notificationTitle, String packageName, String notificationContent, byte[] notificationImage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NOTIFICATION_TITLE_COLUMN, notificationTitle);
contentValues.put(NOTIFICATION_PACKAGE_COLUMN, packageName);
contentValues.put(NOTIFICATION_CONTENT_COLUMN, notificationContent);
contentValues.put(NOTIFICATION_IMAGE, notificationImage);
db.insert(NOTIFICATION_TABLE_NAME, null, contentValues);
return true;
}
public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, NOTIFICATION_TABLE_NAME);
return numRows;
}
/*Method to update notification*/
public boolean updateNotification(Integer id, String notificationTitle, String packageName, String notificationContent, byte[] notificationImage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NOTIFICATION_TITLE_COLUMN, notificationTitle);
contentValues.put(NOTIFICATION_PACKAGE_COLUMN, packageName);
contentValues.put(NOTIFICATION_CONTENT_COLUMN, notificationContent);
contentValues.put(NOTIFICATION_IMAGE, notificationImage);
db.update(NOTIFICATION_TABLE_NAME, contentValues, NOTIFICATION_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
return true;
}
public Integer deleteNotification(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(NOTIFICATION_TABLE_NAME,
NOTIFICATION_COLUMN_ID + " = ? ",
new String[]{Integer.toString(id)});
}
/*Method to get single notification*/
public Cursor getNotification(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + NOTIFICATION_TABLE_NAME + " WHERE " +
NOTIFICATION_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
return res;
}
/*Method to get all notification*/
public Cursor getAllNotifications() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + NOTIFICATION_TABLE_NAME, null);
return res;
}
/**
* Remove a Notification from database by title
*
* @param title to remove
*/
public void removeSingleNotification(String title) {
//Open the database
SQLiteDatabase database = this.getWritableDatabase();
//Execute sql query to remove from database
//NOTE: When removing by String in SQL, value must be enclosed with ''
database.execSQL("DELETE FROM " + NOTIFICATION_TABLE_NAME + " WHERE " + NOTIFICATION_TITLE_COLUMN + "= '" + title + "'");
//Close the database
database.close();
}
/**
* //This method returns all notification from the database
*/
public ArrayList<NotficationModel> getAllNotificationsData() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * from " + NOTIFICATION_TABLE_NAME,
new String[]{});
ArrayList<NotficationModel> listItems = new ArrayList<NotficationModel>();
if (cursor.moveToFirst()) {
do {
NotficationModel model = new NotficationModel();
model.setNotiifcationTitle(cursor.getString(cursor.getColumnIndex(NOTIFICATION_TITLE_COLUMN)));
model.setNotificationText(cursor.getString(cursor.getColumnIndex(NOTIFICATION_CONTENT_COLUMN)));
model.setNotificationPackageName(cursor.getString(cursor.getColumnIndex(NOTIFICATION_PACKAGE_COLUMN)));
model.setNotificationImage(cursor.getBlob(cursor.getColumnIndex(NOTIFICATION_IMAGE)));
listItems.add(model);
} while (cursor.moveToNext());
}
cursor.close();
return listItems;
}
code of Notification service
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("Msg", "Notification Posted");
String pack = sbn.getPackageName();
String ticker = "";
if (sbn.getNotification().tickerText != null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = null;
if (extras.getCharSequence("android.text") != null)
{
text = extras.getCharSequence("android.text").toString();
}
Bitmap id = sbn.getNotification().largeIcon;;
if (id != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}else {
Toast.makeText(context, "Bitmap came null", Toast.LENGTH_SHORT).show();
}
String jsonData = sharedPreference.getAppsArrayListData();
Type type = new TypeToken<ArrayList<WhiteListModel>>() {}.getType();
whiteListModels = gson.fromJson(jsonData, type);
if (whiteListModels != null && whiteListModels.size()>0)
//loop to insert notification data into db.
{
for (int i = 0; i < whiteListModels.size(); i++) {
model = whiteListModels.get(i);
if (pack.equals(model.getPackName())) {
dbHelper.insertNotification(title, pack, text, byteArray);
}
}
//loop to remove notification from status bar.
for (int i = 0; i < whiteListModels.size(); i++) {
model = whiteListModels.get(i);
if (pack.equals(model.getPackName())) {
NotificationService.this.cancelNotification(sbn.getKey());
}
}
}
}
code for adapter:-
private LayoutInflater layoutInflater;
Context context;
ArrayList<NotficationModel> modelList;
public NotificationStorageActivityAdapter(Context context, ArrayList<NotficationModel> modelList) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.modelList = modelList;
}
@Override
public int getCount() {
return modelList.size();
}
@Override
public Object getItem(int position) {
return modelList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final NotificationStorageActivityAdapter.ViewHolder listViewHolder;
if (convertView == null) {
listViewHolder = new NotificationStorageActivityAdapter.ViewHolder();
convertView = layoutInflater.inflate(R.layout.notification_list_item, parent, false);
listViewHolder.txtTitle = (TextView) convertView.findViewById(R.id.notification_title);
listViewHolder.imageView = (ImageView) convertView.findViewById(R.id.notification_icon);
listViewHolder.text = (TextView) convertView.findViewById(R.id.notification_text);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (NotificationStorageActivityAdapter.ViewHolder) convertView.getTag();
}
NotficationModel m = modelList.get(position);
listViewHolder.txtTitle.setText(m.getNotiifcationTitle());
listViewHolder.text.setText(m.getNotificationText());
byte[] outImage = m.getNotificationImage();
Bitmap theImage = null;
if (outImage != null) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
theImage = BitmapFactory.decodeStream(imageStream);
listViewHolder.imageView.setImageBitmap(theImage);
}
return convertView;
}
private static class ViewHolder {
SwitchCompat switchCompat;
TextView txtTitle, text;
ImageView imageView;
}
}