The better way to achieve what you want is to create custom adapter. Here I am giving you an example to achieve your goal...
Activity Layout---> activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
List Item Layout---> listview_routes_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/long_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/short_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip" />
</LinearLayout>
Custom SimpleAdapte---> CustomSimpleAdapter.java
public class CustomSimpleAdapter extends SimpleAdapter {
private List<Map<String, Object>> itemList;
private Context mContext;
private static final String TAG_COLOR = "color";
private static final String TAG_NAME = "shop_name";
private static final String TAG_SHORT = "shop_address";
public CustomSimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.itemList = (List<Map<String, Object>>) data;
this.mContext = context;
}
/* A Static class for holding the elements of each List View Item
* This is created as per Google UI Guideline for faster performance */
class ViewHolder {
TextView textLong;
TextView textShort;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_routes_row, null);
holder = new ViewHolder();
// get the textview's from the convertView
holder.textLong = (TextView) convertView.findViewById(R.id.long_name);
holder.textShort = (TextView) convertView.findViewById(R.id.short_name);
// store it in a Tag as its the first time this view is generated
convertView.setTag(holder);
} else {
/* get the View from the existing Tag */
holder = (ViewHolder) convertView.getTag();
}
/* update the textView's text and color of list item */
holder.textLong.setText((CharSequence) itemList.get(position).get(TAG_NAME));
holder.textShort.setText((CharSequence) itemList.get(position).get(TAG_SHORT));
holder.textShort.setTextColor((Integer) itemList.get(position).get(TAG_COLOR));
return convertView;
}
}
Activity Class---> MainActivity.java
public class MainActivity extends Activity {
private static final String TAG_NAME = "shop_name";
private static final String TAG_SHORT = "shop_address";
private static final String TAG_COLOR = "color";
private List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
private Map<String, Object> map;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
//Sample data insertion into the list
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview11");
map.put(TAG_SHORT, "textview12");
map.put(TAG_COLOR, Color.BLUE);
itemList.add(map);
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview21");
map.put(TAG_SHORT, "textview22");
map.put(TAG_COLOR, Color.GREEN);
itemList.add(map);
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview31");
map.put(TAG_SHORT, "textview32");
map.put(TAG_COLOR, Color.RED);
itemList.add(map);
/* create an adapter for listview*/
SimpleAdapter adapter = new CustomSimpleAdapter(this, itemList,
R.layout.listview_routes_row, new String[] { TAG_NAME, TAG_SHORT }, new
int[] { R.id.long_name, R.id.short_name });
listView.setAdapter(adapter);
}
}
I think it will help you. If you have any problem then let me know.