问题
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