Android, SpannableString, ArrayAdapter and Listview

早过忘川 提交于 2020-01-14 05:51:13

问题


i am newbie to android and i got a question for you. 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. 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.

Any ideas?

Thanks..


回答1:


There is an example for custom list adapter in this post: 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



来源:https://stackoverflow.com/questions/15273771/android-spannablestring-arrayadapter-and-listview

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