I'm feeding data between databases in my Android app. One database's information appears in a textview as clickable spans. A cursor reorders all spans when a new span is created before they are all re-appended to the textview.
This functionality works, but iterating through a cursor every time the data changes over a short period of time is noticeably slow once the database has around 50 or more rows.
Is there a way to implement an approach that only needs to look at the new row to add it in its proper place, instead of iterating through every row in a cursor every single time a new span is created? Similarly, is there a way to remove the span without having to reload the entire textview?
Code for preparing data for textview consisting of clickableSpans:
Cursor cursor = dbHelper.fetchAllTT3Values();
do {
String value = cursor.getString(cursor.getColumnIndex("mainValues"));
doClickSpanForString(tv1, value, cursor.getInt(3));
} while (cursor.moveToNext());
cursor.close();
(Note: the dbHelper.fetchAllTT3Values() function orders data by a column of integers, so this rearranges what is appended in the function below.)
Code for clickable spans tagged with data:
public void doClickSpanForString(TextView txtSpan, String valtag, Integer valordtag) {
spanText = new SpannableString(valtag+" ");
MyClickableSpan a = new MyClickableSpan(valtag);
a.setTag1(valtag);
a.setTag2(valordtag);
spanText.setSpan(a, 0, valtag.length(), 0);
txtSpan.append(spanText);
}
private class MyClickableSpan extends ClickableSpan {
private String mtag1;
private int mtag2;
void setTag1(String tag) {
mtag1 = tag;
}
String getTag1() {
return mtag1;
}
void setTag2(Integer tag) {
mtag2 = tag;
}
int getTag2() {
return mtag2;
}
MyClickableSpan(String string) {
super();
}
public void onClick(View tv) {
//tv.performLongClick();
setstr4val(getTag1());
setint4valord(getTag2());
}
}