Why can't use the same Span object to setSpan above twice?

android

Solution

I suspect that the span objects wind up as keys of a `HashMap`, inside the `Spanned` representation. Hence, reusing the same span object has the effect of replacing its prior use with a new use.

Problem

Why can't use the same Span object to setSpan above twice? SpannableString ss = new SpannableString("aaaaa[1]bbbb[1]cccc[1]"); I need to replace all the [1] with a image. If I use the following code, only the last one is replaced by the image: ``` etShow = (EditText) findViewById(R.id.show); SpannableString ss = new SpannableString("aaaaa[1]bbbb[1]cccc[1]"); int[] starts = new int[3]; int[] ends = new int[3]; int h = 0; int k = 0; for (int i = 0; i < ss.length(); i++) { if (ss.charAt(i) == '[') { starts[h] = i; h++; } else if (ss.charAt(i) == ']') { ends[k] = i; k++; } } Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); ImageSpan im = new ImageSpan(d); for(int i=0;i<3;i++){ ss.setSpan(im, starts[i], ends[i]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } etShow.getText().insert(0, ss); ``` If change to the following code, all the [1] are replaced by the image. ``` Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); ImageSpan im = new ImageSpan(d); ImageSpan im1 = new ImageSpan(d); ImageSpan im2 = new ImageSpan(d); //for(int i=0;i<3;i++){ // ss.setSpan(im, starts[i], ends[i]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im, starts[0], ends[0]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im1, starts[1], ends[1]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im2, starts[2], ends[2]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); // } ``` How can explaint this?

Original source