See following class
package com.example;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListWithImage extends ListActivity {
String[] scrips;
//for running task in background
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
setListAdapter(new Scripdapter(getApplicationContext(), R.layout.main, scrips));
}
};
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scrips = getResources().getStringArray(R.array.scrip);
upadateList();
}
public void upadateList() {
Thread t = new Thread(new Runnable() {
public void run() {
handler.handleMessage(handler.obtainMessage());
}
});
t.start();
}
class Scripdapter extends ArrayAdapter {
private String[] items;
Scripdapter(Context context, int textViewResourceId, String[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.main, null);
}
String item = items[position];
if (item != null) {
TextView txtV = (TextView) row.findViewById(R.id.scripList);
ImageView imgV = (ImageView) row.findViewById(R.id.scripimage);
if(txtV != null){
txtV.setText(item);
}
if (item.equals("sbi") || item.equals("unitech")) {
imgV.setImageResource(R.drawable.red);
} else if (item.equals("BNP paribas") || item.equals("wipro")) {
imgV.setImageResource(R.drawable.green);
} else if (item.equals("dlf") || item.equals("nokia")) {
imgV.setImageResource(R.drawable.green);
} else {
imgV.setImageResource(R.drawable.icon);
}
}
return row;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px"
android:id="@+id/scripimage">
</ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scripList"
android:textSize="30px"></TextView>
</LinearLayout>
string.xml
<resources>
<string name="app_name">ContactsView</string>
<string-array name="scrip">
<item>sbi</item>
<item>Geojit</item>
<item>BNP paribas</item>
<item>dlf</item>
<item>unitech</item>
<item>suzlon</item>
<item>axis bank</item>
<item>wipro</item>
<item>infosis</item>
<item>nokia</item>
</string-array>
</resources>
output
package com.example;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListWithImage extends ListActivity {
String[] scrips;
//for running task in background
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
setListAdapter(new Scripdapter(getApplicationContext(), R.layout.main, scrips));
}
};
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scrips = getResources().getStringArray(R.array.scrip);
upadateList();
}
public void upadateList() {
Thread t = new Thread(new Runnable() {
public void run() {
handler.handleMessage(handler.obtainMessage());
}
});
t.start();
}
private String[] items;
Scripdapter(Context context, int textViewResourceId, String[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.main, null);
}
String item = items[position];
if (item != null) {
TextView txtV = (TextView) row.findViewById(R.id.scripList);
ImageView imgV = (ImageView) row.findViewById(R.id.scripimage);
if(txtV != null){
txtV.setText(item);
}
if (item.equals("sbi") || item.equals("unitech")) {
imgV.setImageResource(R.drawable.red);
} else if (item.equals("BNP paribas") || item.equals("wipro")) {
imgV.setImageResource(R.drawable.green);
} else if (item.equals("dlf") || item.equals("nokia")) {
imgV.setImageResource(R.drawable.green);
} else {
imgV.setImageResource(R.drawable.icon);
}
}
return row;
}
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px"
android:id="@+id/scripimage">
</ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scripList"
android:textSize="30px"></TextView>
</LinearLayout>
string.xml
<resources>
<string name="app_name">ContactsView</string>
<string-array name="scrip">
<item>sbi</item>
<item>Geojit</item>
<item>BNP paribas</item>
<item>dlf</item>
<item>unitech</item>
<item>suzlon</item>
<item>axis bank</item>
<item>wipro</item>
<item>infosis</item>
<item>nokia</item>
</string-array>
</resources>
output