Hey guys so I am having a problem with my code. When I call upon sqliteadapter to retrieve information for each view stored in my adapter it causes the program to become unbearably sluggish when the user tries to scroll through the BaseAdapter's views. How can I improve my code to eliminate any lag or crashes to occur while the user is using the NotificationAdapter without adding additional columns to any tables?
SQLiteAdapter:
public class SQLiteAdapter {
private static final int DATABASE_VERSION = 1;
public static SQLiteHelper sSqLiteHelper;
private static String DB_PATH;//= Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/";
private Runnable runnable;
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 int getTagID(final int lawID){
int tagID = 0;
openToRead();
String Query = "SELECT * from " + Constants.TABLE_LAW_TAG;
Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
try {
if (cursor.getInt(cursor.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
int indexTagID = cursor.getColumnIndex(Constants.KEY_TAG_ID);
tagID = cursor.getInt(indexTagID);
}
} catch (Exception e) {
exceptionHandler.alert(e, "getTagID()");
}
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 String getTagNameByLawId(int lawID){
int tagID = getTagID(lawID);
String tagName = getTagName(tagID);
return tagName;
}
}
NotificationAdapter:
public class NotificationAdapter extends BaseAdapter implements SQLiteAdapter.SQLiteDbQueryListener, SeekBar.OnSeekBarChangeListener{
private List<NotificationModel> lstNotificationModels;
private SQLiteAdapter sqLiteAdapter;
private ExceptionHandler exceptionHandler;
private Context context;
private boolean textSelected;
private boolean typeSelected;
private FooterLayout footerLayout;
public NotificationAdapter(Context context) {
this.context = context;
sqLiteAdapter=new SQLiteAdapter(context, this, new Dialog(context));
exceptionHandler = new ExceptionHandler(context, "NotificationAdapter");
footerLayout = new FooterLayout(context);
}
public void setList(List<NotificationModel> genres) {
this.lstNotificationModels = genres;
notifyDataSetChanged();
}
private class ViewHolder {
TextView mNotifiactionTypeTv, mNotifiactionTextTv, mNotifiactionTimeTv;
LinearLayout rowNotificationLl;
}
@Override
public int getCount() {
return null == lstNotificationModels ? 0 : lstNotificationModels.size();
}
@Override
public Object getItem(int position) {
return lstNotificationModels.get(position);
}
@Override
public long getItemId(int position) {
return lstNotificationModels.indexOf(getItem(position));
}
@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 = AtlasApplication.lstNotificationModels.get(position); notificationModel.setmNotificationText(sqLiteAdapter.getTagNameByLawId(notificationModel.getmLawId()) + " has changed in your current state.");
notifyDataSetChanged();
holder.mNotifiactionTextTv.setText(Html.fromHtml(notificationModel.getmNotificationText().trim()));
return convertView;
}
}
NotificationActivity:
public class NotificationActivity extends BaseActivity implements SQLiteAdapter.SQLiteDbQueryListener {
public static final String TAG = LoginActivity.class.getSimpleName();
private NotificationAdapter notificationAdapter;
private HeaderLayout headerLayout;
private FooterLayout footerLayout;
private SimpleGestureFilter detector;
private ListView mNotificationLv;
private SQLiteAdapter sqLiteAdapter;
private ExceptionHandler exceptionHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
exceptionHandler = new ExceptionHandler(this, TAG);
try {
super.onCreate(savedInstanceState);
sqLiteAdapter = new SQLiteAdapter(this, this, new Dialog(this));
setContentView(R.layout.activity_notification);
mNotificationLv = (ListView) findViewById(R.id.notification_lv);
headerLayout = new HeaderLayout(this);
footerLayout = new FooterLayout(this);
footerLayout.setNotificationMode();
notificationAdapter = new NotificationAdapter(this);
final List<NotificationModel> userModelList = AtlasApplication.lstNotificationModels;
notificationAdapter.notifyDataSetChanged();
notificationAdapter.setList(userModelList);
mNotificationLv.setAdapter(notificationAdapter);
}
catch(Exception e){
exceptionHandler.alert(e, "onCreate();");
}
}
@Override
protected void onResume() {
try {
super.onResume();
FontLoader.loadFonts(this);
headerLayout.LoadHeaderFont();
footerLayout.LoadFooterFont();
notificationAdapter.notifyDataSetChanged();
List<NotificationModel> userModelList = AtlasApplication.lstNotificationModels;
notificationAdapter = new NotificationAdapter(this);
notificationAdapter.setList(userModelList);
//mNotificationLv.setAdapter(notificationAdapter);
footerLayout = new FooterLayout(this);
}
catch(Exception e){
exceptionHandler.alert(e, "onResume();");
}
}
public static Intent getIntent(Context context) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
}
Aucun commentaire:
Enregistrer un commentaire