简体   繁体   中英

How to set icons for a list view items on android

I want to add some icons for every items in my list view in my android application.

mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1,
new String[] {
    getString(R.string.title_section1),
    getString(R.string.title_section2),
    getString(R.string.title_section3),
    getString(R.string.title_section4),
    getString(R.string.title_section5),
    getString(R.string.title_section6)}) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getView(position, convertView, parent);
    textView.setTextColor(getResources().getColor(R.color.dark_grey));
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    return textView;
}
});

Should I add it on my getView function?

As @ckpatel mentioned you can make a custom adapter see this tutorial(preferred) or set drawableRight programatically to each of the textview just like you set the textcolor.

Drawable image =myContext.getResources().getDrawable( R.drawable.ic_launcher);
textView.setCompoundDrawablesWithIntrinsicBounds( null, null, image, null);

try this code for icon

TextView lbl = (TextView) convertView;
        lbl.setText(f.getTitle());
        lbl.setCompoundDrawablesWithIntrinsicBounds(f.getImage(), 0, 0, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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