简体   繁体   English

Android,SpannableString,ArrayAdapter和Listview

[英]Android, SpannableString, ArrayAdapter and Listview

i am newbie to android and i got a question for you. 我是android的新手,所以有一个问题要问您。 I want to add icon to a text in a listview. 我想在列表视图的文本中添加图标。 I'm adding the rows(textview) to listview dynamically with below code. 我使用以下代码将行(文本视图)动态添加到列表视图。 But the text doesnt contain the icon. 但是文本不包含图标。

.
.
.
btnicon = (Button)findViewById(R.id.button1);
txt = (TextView)findViewById(R.id.textView1);
txt.setText("   denemedene",BufferType.SPANNABLE);
list = (ListView)findViewById(R.id.listview);
dizi = new ArrayAdapter<String>(this, R.layout.text);
list.setAdapter(dizi);
btnicon.setOnClickListener(click);
.
.
.
private OnClickListener click = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v == btnicon){
            SpannableString spans = new SpannableString(txt.getText());
            Drawable dra = getResources().getDrawable(R.drawable.p1);
            dra.setBounds(0, 0, dra.getIntrinsicWidth() , dra.getIntrinsicHeight());
            ImageSpan span = new ImageSpan(dra,ImageSpan.ALIGN_BASELINE);
            spans.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            dizi.add(spans.toString());
        }
    }
};

it is something about the toString method. 它与toString方法有关。 Because when i change the 因为当我改变

dizi.add(spans.toString());

line with 符合

txt.setText(spans,BufferType.SPANNABLE);

it works, i can see the icon on text. 它有效,我可以在文本上看到该图标。 I have no idea why it is not working with toString() method. 我不知道为什么它不能与toString()方法一起使用。

Any ideas? 有任何想法吗?

Thanks.. 谢谢..

There is an example for custom list adapter in this post: https://stackoverflow.com/a/15272092/1752867 这篇文章中有一个自定义列表适配器的示例: https : //stackoverflow.com/a/15272092/1752867

You can create your custom adapter like that and change checkbox component with imageView or ProgressBar in the layout 您可以像这样创建自定义适配器,并在布局中使用imageView或ProgressBar更改复选框组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM