Android - customize ListView to show text and images
When you work with a ListView in Android to show a list of items to the user, you might want every item or row of the ListView contains both text and icon. This tip shows you how to achieve this goal.
Now create a new Android project in Eclipse ADT. Then modify the activity_main.xml file to add a ListView as shown below.
The ListView contains its own layout file that defines the template for every item of the ListView. In that file, you can customize an item of the ListView. In this tip, every item of the ListView contains two parts: icon and text. So, the layout file (listlayout.xml) is written as below:
After the template for every item of the ListView is defined, you will create a class that its object is used as the data source of the list. This class (ListAdapterModel) will extends the ArrayAdapter class. You need to override the getView method of the ArrayAdapter class to add data to every item of the list.
public class ListAdapterModel extends ArrayAdapter<String>{ int groupid; String[] clist; Context context;
public ListAdapterModel(Context context, int vg, int id, String[] clist){ super(context,vg, clist); this.context=context; groupid=vg; this.clist=clist;
} public View getView(int position, View convertView, ViewGroup parent) {
Next, in the onStart method of MainActivity class, you need to create a reference to the ListView, create an object of the ListAdapterModel, set the object to the ListView witht the setAdapter method.
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.