phpdroid phpdroid - 1 month ago 12
Android Question

Spannable string returning whole string on click

Using Button on a row in the

ListView
and am able to make a single row Spannable and data from database but when i click it is returning whole string than a word here is my code from
MyAdapter Class
where am entering values from database to
Listview



public View getView(int i, View view, ViewGroup viewGroup) {
Realm realm=Realm.getDefaultInstance();
Word toEdit = realm.where(Word.class)
.equalTo("id", 10).findFirst();
int id_to_seperate=toEdit.getLang();

LayoutInflater inflater= (LayoutInflater) Main.context.getSystemService(Main.context.LAYOUT_INFLATER_SERVICE);
View row= inflater.inflate(R.layout.layout,viewGroup,false);
TextView word= (TextView) row.findViewById(R.id.word_name);
TextView meaning= (TextView) row.findViewById(R.id.word_define);
Word temp=list.get(i);
int idz=temp.getId();
word.setText(temp.getWord());
if(id_to_seperate==idz){
String span[] = temp.getMeaning().substring(1).split(" ") ;
SpannableString ss = new SpannableString(temp.getMeaning().substring(1));
ClickableSpan spans[] = new ClickableSpan[span.length];
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
@Override
public void onClick(View textView) {

TextView v = (TextView)textView ;
String text = v.getText().toString() ;

Log.d("View" , text);
}
};
}

int start = 0 ;
int end =0;
try {
for(int spanCount = 0 ; spanCount <span.length ; spanCount++){
if(spanCount==0) {
end += span[spanCount].length();
}else{

end += span[spanCount].length()+1;
}
ss.setSpan(spans[spanCount], start, end , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += span[spanCount].length()+1;

}
} catch (Exception e) {
e.printStackTrace();
}

meaning.setText(ss);
meaning.setMovementMethod(LinkMovementMethod.getInstance());

}else {
meaning.setText(temp.getMeaning().substring(1));
}
return row;
}


am able to get result from a specific row in a listenter image description here

to this enter image description hereBut click Functionality returning whole sentense than a span or word, I would be thankful if anyone can tell me what mistake I have done

Regards

Answer

here is wrong in your code

TextView v = (TextView)textView ;
String text = v.getText().toString() ;

TextView.getText().toString() is return full text. if you want to get specific ClickableSpan then use below ClickableSpan

public class SpecialClickableSpan extends ClickableSpan {

    String text;

    public SpecialClickableSpan(String text){
         super();
         this.text = text;
    }

    @Override
    public void onClick(View widget) {
         Log.d(TAG, "onClick [" + text + "]");
    }
}

replace

for(int spanCount = 0 ; spanCount < span.length ; spanCount++){

    spans[spanCount] = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            TextView v = (TextView)textView ;
            String text = v.getText().toString() ;
                Log.d("View" , text);
            }
        };
    }

with

for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
    spans[spanCount] =  new SpecialClickableSpan (span[spanCount ]);
}
Comments