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());
}
}
Edit:
I ended up not using the method below by querying data only once from the database during use of that database's data. It's way faster, but after about a thousand pieces of data are added to the textview, it's still noticeably slow. Either attaching a large spannablestringbuilder to textview or updating data beforehand via loops is contributing to the slowness. I think removing the loops is the only solution. But that won't make attaching data to textview any faster, if that attachment is a bottleneck. WHAT FOLLOWS IS AN OLD, INSUFFICIENT SOLUTION.
Changing the function doClickSpanForString to not act on the textView at every iteration of the loop made this code much more efficient. But it's still slow.