currently i’m trying to get a custom spinner to work. It uses a custom layout and the arrayadapter of it gets filled by custom objects. now i want a icon to appear next to every object in the spinner.
so far i use code that looks like this:
// create spinner
logicSpinner = (Spinner) view.findViewById(R.id.logicSpinner);
logicAdapter = new ArrayAdapter<SearchLogic>(parent, R.layout.icon_spinner_layout, R.id.typesfield, myLogics);
logicSpinner.setAdapter(logicAdapter);
where myLogics provides a ArrayList with types of SearchLogic. The SearchLogic class looks like this
public class SearchLogic {
private String otherString;
private String localString;
private int imageRes;
public SearchLogic(String otherString, String localString, int imageRes){
this.otherString = otherString;
this.localString = localString;
this.imageRes = imageRes;
}
/*
* let's override the toString method since thats the method a spinner object uses as default value for its entries
*/
@Override
public String toString(){
return this.localString;
}
The icon_spinner_layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ObjectSpecificIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/typesfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
now i’m creating a new SearchLogic object and adding it to myLogics like so:
myLogics.add(new SearchLogic("string1", "string2", R.drawable.iconXYZ));
and i would like that “ObjectSpecificIcon” is filled with the icon passed to the SearchLogic object. How to do that? is it possible to provide a “placeholder” within the layout.xml file instead of providing a hard-coded src? or any other way?
here is how its done: Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder
basicly you have to use a custom ViewBinder for the Array / CursorAdapter. See my own answer to get the final problems i had while implementing it resolved.