可自定义的字符串在自定义ListView

时间:2016-09-04 04:58:36

标签: java android listview spannablestring

ListView的行中使用按钮,并且能够创建单行Spannable和来自数据库的数据但是当我点击它时没有响应点击这里是我MyAdapter Class的代码我在哪里从数据库输入值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;
}

我能够从列表enter image description here中的特定行获得结果 这个enter image description here但是点击功能不起作用,如果有人能告诉我我做了什么错误,我将感激不尽 此致

1 个答案:

答案 0 :(得分:1)

return添加

之前
meaning.setMovementMethod(LinkMovementMethod.getInstance());

此方法在链接上启用了clicable。