jjzjj

java - 为什么我的 android Activity 执行如此缓慢?

coder 2023-12-30 原文

从您单击通知 Activity 按钮的那一刻起,程序立即开始缓慢运行。由于某种原因,此屏幕需要几分钟才能以非常故障和拖长的方式向下滚动。我可以做些什么来加快和平滑我的通知 Activity 屏幕?

通知 Activity :

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;
}
}

SQLite 适配器:

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;
}

主要 Activity :

sqLiteAdapter.updateLaw(962, "test", "test");

最佳答案

您描述的问题很难在此代码中检测到,但这里列出了您应该考虑的注意事项/最佳实践:

  • 您应该始终在辅助线程而不是主线程上执行数据库查询(尝试 AsyncTask)。
  • 你有一个 SimpleGestureFilter,我不能说它是做什么用的。如果那里有一些丑陋的逻辑,那可能会影响滚动。
  • 您在 getView() 方法中创建了大量 LayoutInflater 变量,这非常糟糕。使它成为一个全局变量并在 Adapter 的构造函数中对其进行初始化。您知道滚动时 getView() 方法被调用了多少次吗?将日志放在那里并分析 :)。
  • 绝对不要在适配器中为您的 SQLiteAdapter 对象保留一个实例。在您的 Activity 中执行此操作并在需要时更新您的适配器。适配器应始终代表您的数据,而不是做任何逻辑和其他事情。
  • 不确定 FontLoader 的作用,但如果您每次调用 getView() 时都从 Assets 中读取字体,那么您会遇到一个很大的问题。尝试尽可能少地加载字体,因为这是一项繁重的操作。
  • 从不捕获一般异常。尝试专注于特定的问题。

关于java - 为什么我的 android Activity 执行如此缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32508029/

有关java - 为什么我的 android Activity 执行如此缓慢?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  7. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  8. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

  9. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  10. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

随机推荐