从你点击通知活动按钮的那一刻起,程序立即开始变得缓慢。这个屏幕由于某些原因需要花费几分钟才能向下滚动,而且还会出现很多故障和拖延。我该怎么做才能加快并使我的通知活动屏幕更加流畅?
通知活动:
public class NotificationActivity extends BaseActivity {
public static final String TAG = LoginActivity.class.getSimpleName();
private NotificationAdapter notificationAdapter;
private HeaderLayout headerLayout;
private FooterLayout footerLayout;
private SimpleGestureFilter detector;
private ListView mNotificationLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
mNotificationLv = (ListView) findViewById(R.id.notification_lv);
mNotificationLv = (ListView) findViewById(R.id.notification_lv);
notificationAdapter = new NotificationAdapter(this);
notificationAdapter.setList(AtlasApplication.lstNotificationModels);
mNotificationLv.setAdapter(notificationAdapter);
// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
@Override
protected void onResume() {
super.onResume();
List<NotificationModel> userModelList = AtlasApplication.lstNotificationModels;
notificationAdapter = new NotificationAdapter(this);
notificationAdapter.setList(userModelList);
mNotificationLv.setAdapter(notificationAdapter);
}
}
通知适配器:
public class NotificationAdapter extends BaseAdapter{
private List<NotificationModel> lstNotificationModels;
private SQLiteAdapter sqLiteAdapter;
private Context context;
public NotificationAdapter(Context context) {
this.context = context; sqLiteAdapter=new SQLiteAdapter(context, this, new Dialog(context));
}
public void setList(List<NotificationModel> genres) {
this.lstNotificationModels = genres;
notifyDataSetChanged();
}
private class ViewHolder {
TextView mNotifiactionTypeTv, mNotifiactionTextTv, mNotifiactionTimeTv;
LinearLayout rowNotificationLl;
}
public void setRead(int i){
sqLiteAdapter.updateNotificationStatus(i, "read");
lstNotificationModels.get(i).setmNotificationStatus("read");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_notification, null);
holder = new ViewHolder();
holder.rowNotificationLl = (LinearLayout) convertView.findViewById(R.id.row_notification_ll);
holder.mNotifiactionTextTv = (TextView) convertView.findViewById(R.id.notification_text_tv);
holder.mNotifiactionTimeTv = (TextView) convertView.findViewById(R.id.notification_time_tv);
holder.mNotifiactionTypeTv = (TextView) convertView.findViewById(R.id.notification_type_atv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final NotificationModel notificationModel = lstNotificationModels.get(position);
final String newFullText = sqLiteAdapter.getFullText(notificationModel.getmLawId());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = new Date();
Date notificationDate = new Date();
String timeElapsed = "moments ago.";
try {
notificationDate = simpleDateFormat.parse(notificationModel.getmNotificationTime().trim());
} catch(ParseException e){
}
long milliElapsed = currentDate.getTime() - notificationDate.getTime() + 14400000;
if(milliElapsed>=60000){
long minutesElapsed = milliElapsed/60000;
timeElapsed = minutesElapsed + " minutes ago.";
if(minutesElapsed>=60){
long hoursElapsed = minutesElapsed/60;
timeElapsed = hoursElapsed + " hours ago.";
if(hoursElapsed>=24){
long daysElapsed = hoursElapsed/60;
timeElapsed = daysElapsed + " days ago.";
if(daysElapsed>=7){
long weeksElapsed = daysElapsed/7;
timeElapsed = hoursElapsed + " weeks ago.";
if(weeksElapsed>=4){
long monthsElapsed = weeksElapsed/4;
timeElapsed = monthsElapsed + " months ago.";
if(daysElapsed>=365){
long yearsElapsed = daysElapsed/365;
timeElapsed = yearsElapsed + " years ago.";
}
}
}
}
}
}
holder.mNotifiactionTextTv.setText(Html.fromHtml(notificationModel.getmNotificationText().trim()));
holder.mNotifiactionTimeTv.setText(timeElapsed);
if (notificationModel.getmNotificationStatus().equalsIgnoreCase("unread")) {
convertView.findViewById(R.id.unread_vw).setVisibility(View.VISIBLE);
holder.rowNotificationLl.setBackgroundResource(R.color.black);
}
else {
convertView.findViewById(R.id.unread_vw).setVisibility(View.GONE);
holder.rowNotificationLl.setBackgroundResource(R.color.grad_light);
}
switch (notificationModel.getmNotificationType().toLowerCase()){
case "traffic":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.traffic_noti_bg);
holder.mNotifiactionTypeTv.setText("f");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_TRAFFIC;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(0);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_TRAFFIC;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(0);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
catch(Exception e){
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
case "law enforcement":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.enforcement_noti_bg);
holder.mNotifiactionTypeTv.setText("c");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_ENFORCEMENT;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(1);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_ENFORCEMENT;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(1);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
catch(Exception e){
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
case "alcohol":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.alcohal_noti_bg);
holder.mNotifiactionTypeTv.setText("a");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_ALCOHOL;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(2);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_ALCOHOL;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(2);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
catch(Exception e){
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
case "taxes":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.taxes_noti_bg);
holder.mNotifiactionTypeTv.setText("e");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_TAXES;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(3);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_TAXES;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(3);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
case "guns":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.guns_noti_bg);
holder.mNotifiactionTypeTv.setText("b");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_GUNS;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(4);
setRead(notificationModel.getmNotificaticationId());
AtlasApplication.lstLawsForLocation.get(1).setSelected(true);
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_GUNS;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(4);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
catch(Exception e){
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
case "marijuana":
holder.mNotifiactionTypeTv.setBackgroundResource(R.drawable.marijuana_noti_bg);
holder.mNotifiactionTypeTv.setText("d");
holder.mNotifiactionTypeTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_MARIJUANA;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(5);
setRead(notificationModel.getmNotificaticationId());
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
holder.mNotifiactionTextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
AtlasApplication.MenuTitle = Constants.CAT_MARIJUANA;
AtlasApplication.sCategoryModel = AtlasApplication.lstCategoryModels.get(5);
setRead(notificationModel.getmNotificaticationId());
AtlasApplication.lstLawsForLocation.get(1).setSelected(true);
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
} catch (Exception e) {
view.getContext().startActivity(SubMenuActivity.getIntent(view.getContext()));
}
}
});
break;
default:
break;
}
FontLoader.setAtlasFont(holder.mNotifiactionTypeTv);
FontLoader.setRalewayRegularFont(holder.mNotifiactionTextTv, holder.mNotifiactionTimeTv);
holder.rowNotificationLl.setId(position);
holder.rowNotificationLl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return convertView;
}
}
SQLiteAdapter:
public class SQLiteAdapter {
private static final int DATABASE_VERSION = 1;
private static String DB_PATH;//= Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/";
private SQLiteDatabase mSqLiteDatabase;
private Context mContext;
private Dialog mDialog;
private SQLiteDbQueryListener sqLiteDbQueryListener;
private ExceptionHandler exceptionHandler;
public SQLiteAdapter(Context c, SQLiteDbQueryListener listener, Dialog dialog) {
mContext = c;
sqLiteDbQueryListener = listener;
exceptionHandler = new ExceptionHandler(mContext, "SQLiteAdapter");
mDialog = dialog;
DB_PATH = Environment.getExternalStorageDirectory() + "/" + mContext.getPackageName() + "/";
//call it so db get copied from assets to sdcard
//call it so db get copied from assets to sdcard
openToRead();
close();
}
public void updateLaw(int lawID, String newSummary, String newFullText){
int tagID = getTagID(lawID);
String tagName = getTagName(tagID);
int categoryID = getCategoryID(tagID);
String categoryName = getCategoryName(categoryID);
String location;
location = getLocationName(getLocationID(lawID));
if (location.toLowerCase().equals(AtlasApplication.sHometownSelected.getLocationName().toLowerCase())) {
location = "your current state";
} else if (location.toLowerCase().equals(AtlasApplication.sHometownSelected.getLocationName().toLowerCase())) {
location = "your home state";
}
openToWrite();
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.KEY_SUMMARY, newSummary);
if(newFullText!=null)
contentValues.put(Constants.KEY_FULL_TEXT, newFullText);
mSqLiteDatabase.update(Constants.TABLE_LAW, contentValues, Constants.KEY_LAW_ID + "=" + lawID, null);
close();
insertNotification(lawID, categoryName, tagName + " has changed in " + location + ".");
}
public int getCategoryID(int tagID){
openToRead();
int categoryID = 0;
String Query = "SELECT * from " + Constants.TABLE_CATEGORY_TAG;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if (cursor.getInt(cursor.getColumnIndex(Constants.KEY_TAG_ID)) == tagID) {
int indexCategoryID = cursor.getColumnIndex(Constants.KEY_CATEGORY_ID);
categoryID = cursor.getInt(indexCategoryID);
}
cursor.moveToNext();
}
}
close();
return categoryID;
}
public String getCategoryName(int categoryID){
String categoryName = "";
openToRead();
String Query = "SELECT * from " + Constants.TABLE_CATEGORY;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if (cursor.getInt(cursor.getColumnIndex(Constants.KEY_CATEGORY_ID)) == categoryID) {
int indexCategoryName = cursor.getColumnIndex(Constants.KEY_CATEGORY_NAME);
categoryName = cursor.getString(indexCategoryName);
}
cursor.moveToNext();
}
}
close();
return categoryName.toLowerCase();
}
public int getLocationID(int lawID){
openToRead();
String Query = "SELECT * from " + Constants.TABLE_LAW_LOCATION;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
int locationID = 0;
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
try {
if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
int indexTagID = cursor.getColumnIndex(Constants.KEY_LOCATION_ID);
locationID = cursor.getInt(indexTagID);
}
} catch (Exception e) {
exceptionHandler.alert(e, "getLocationID()");
}
cursor.moveToNext();
}
}
close();
return locationID;
}
public String getLocationName(int locationID){
openToRead();
String Query = "SELECT * from " + Constants.TABLE_LOCATION;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
String locationName = "";
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LOCATION_ID)) == locationID) {
int indexTagID = cursor.getColumnIndex(Constants.KEY_LOCATION_NAME);
locationName = cursor.getString(indexTagID);
}
cursor.moveToNext();
}
}
close();
return locationName;
}
public int getTagID(int lawID){
openToRead();
String Query = "SELECT * from " + Constants.TABLE_LAW_TAG;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
int tagID = 0;
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
int indexTagID = cursor.getColumnIndex(Constants.KEY_TAG_ID);
tagID = cursor.getInt(indexTagID);
cursor.moveToNext();
}
}
close();
return tagID;
}
public String getTagName(int tagID){
openToRead();
String tagName = "";
String Query = "SELECT * from " + Constants.TABLE_TAG;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
if(cursor.moveToFirst()){
while (cursor.isAfterLast() == false) {
try {
if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_TAG_ID)) == tagID) {
int indexTagName = cursor.getColumnIndex(Constants.KEY_TAG_NAME);
tagName = cursor.getString(indexTagName);
}
} catch (Exception e) {
exceptionHandler.alert(e, "getTagName()");
}
cursor.moveToNext();
}
}
close();
return tagName;
}
public void insertNotification(int lawID, String type, String text){
openToWrite();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.KEY_LAW_ID, lawID);
contentValues.put(Constants.KEY_NOTIFICATION_TYPE, type);
contentValues.put(Constants.KEY_NOTIFICATION_TEXT, text);
contentValues.put(Constants.KEY_NOTIFICATION_STATUS, "unread");
mSqLiteDatabase.insert(Constants.TABLE_NOTIFICATION, null, contentValues);
}
catch(Exception e){
exceptionHandler.alert(e, "insertNotification()");
}
close();
}
public void dropNotifications(){
openToWrite();
try{
mSqLiteDatabase.execSQL("DROP TABLE IF EXISTS notification");
}
catch(Exception e){
}
close();
}
public void updateNotificationStatus(int notificationID, String status){
openToWrite();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.KEY_NOTIFICATION_STATUS, status);
mSqLiteDatabase.update(Constants.TABLE_NOTIFICATION, contentValues, Constants.KEY_NOTIFICATION_ID + "=" + notificationID, null);
}
catch(Exception e){
exceptionHandler.alert(e, "updateNotificationStatus()");
}
close();
}
public String getNotificationStatus(int notificationID){
openToRead();
String Query = "SELECT * FROM " + Constants.TABLE_NOTIFICATION + " WHERE " + Constants.KEY_NOTIFICATION_ID + "=" + notificationID;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
String notificationStatus = "";
if(cursor.moveToFirst()){
while (cursor.isAfterLast() == false) {
try {
if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID)) == notificationID) {
int indexNotificationStatus = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_STATUS);
notificationStatus = cursor.getString(indexNotificationStatus);
}
} catch (Exception e) {
exceptionHandler.alert(e, "getNotificationStatus()");
}
cursor.moveToNext();
}
}
close();
return notificationStatus;
}
public int getNotificationId(int lawID, String time){
openToRead();
int notificationId = 0;
String query = "SELECT * FROM " + Constants.TABLE_NOTIFICATION +
" WHERE " + Constants.KEY_NOTIFICATION_TIME + " = " + time;
Cursor cursor = mSqLiteDatabase.rawQuery(query, null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
int indexNotificationID = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID);
notificationId = cursor.getInt(indexNotificationID);
}
}
close();
return notificationId;
}
public List<NotificationModel> getNotificationList(){
List<NotificationModel> lstNotifications = new ArrayList<NotificationModel>();
openToRead();
String Query = "SELECT * from " + Constants.TABLE_NOTIFICATION;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
try {
int indexNotificationID = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID);
int indexLawID = cursor.getColumnIndex(Constants.KEY_LAW_ID);
int indexNotificationTime = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TIME);
int indexNotificationType = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TYPE);
int indexNotificationText = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TEXT);
int indexNotificationStatus = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_STATUS);
int notificationID = cursor.getInt(indexNotificationID);
int lawID = indexLawID;
String notificationTime = cursor.getString(indexNotificationTime);
String notificationType = cursor.getString(indexNotificationType);
String notificationText = cursor.getString(indexNotificationText);
String notificationStatus = cursor.getString(indexNotificationStatus);
lstNotifications.add(new NotificationModel(notificationID, lawID, notificationType, notificationText, notificationTime, notificationStatus));
} catch (Exception e) {
exceptionHandler.alert(e, "getNotificationList()");
}
cursor.moveToNext();
}
}
close();
Collections.reverse(lstNotifications);
return lstNotifications;
}
}
MainActivity:
sqLiteAdapter.updateLaw(962, "test", "test");