从第二次单击通知活动按钮起,程序立即开始运行缓慢.由于某种原因,此屏幕需要几分钟才能以非常小故障和抽出的方式向下滚动.我该怎么做才能加快并简化通知活动屏幕?
NotificationActivity:
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;@OverrIDeprotected 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);}@OverrIDeprotected voID onResume() { super.onResume(); List<NotificationModel> usermodelList = AtlasApplication.lstNotificationModels; notificationAdapter = new NotificationAdapter(this); notificationAdapter.setList(usermodelList); mNotificationLv.setAdapter(notificationAdapter);}}
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");}@OverrIDepublic 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;}
}
主要活动:
sqliteAdapter.updateLaw(962, "test", "test");
解决方法:
您描述的问题很难在此代码中检测到,但以下是您应考虑的注释/最佳实践列表:
>您应该始终在辅助线程而不是主线程上执行数据库查询(尝试AsyncTask).
>您有一个SimpleGestureFilter,我无法说出它的用途或作用.如果那里有一些丑陋的逻辑,则可能会影响滚动.
>在getVIEw()方法中创建了大量的LayoutInflater变量,这很糟糕.将其设为全局变量,然后在适配器的构造函数中对其进行初始化.您是否知道滚动时会调用getVIEw()方法多少次?将日志放在那里并分析:).
>绝对不将实例保存到适配器中的sqliteAdapter对象.在您的活动中执行此 *** 作,并在需要时更新适配器.适配器应该始终是数据的表示形式,不要具有逻辑或其他任何含义.
>不确定FontLoader的功能,但是如果您每次调用getVIEw()时从资产中读取一种字体,那么就会遇到麻烦.尝试尽可能少地加载字体,因为这是一项繁重的 *** 作.
>切勿捕获一般异常.尝试着重于特定的.
以上是内存溢出为你收集整理的java-为什么我的android活动执行得这么慢?全部内容,希望文章能够帮你解决java-为什么我的android活动执行得这么慢?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)