Spannable string not clickable in a custom ListView

僤鯓⒐⒋嵵緔 提交于 2019-12-13 08:58:28

问题


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 list
to this But click Functionality not working, I would be thankful if anyone can tell me what mistake I have done
Regards


回答1:


Before return add

meaning.setMovementMethod(LinkMovementMethod.getInstance());

This method enabled clicable on link.



来源:https://stackoverflow.com/questions/39313679/spannable-string-not-clickable-in-a-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!