Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

java - Spannable string not clickable in a custom ListView

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 not responding to click 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);

    }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 not working, I would be thankful if anyone can tell me what mistake I have done
Regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Before return add

meaning.setMovementMethod(LinkMovementMethod.getInstance());

This method enabled clicable on link.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...