please help to solve this problem , in this i am trying to get the list of countries in the spinner along with it's flag And here is the code: I have called the spinner object with in the fragment
v=inflater.inflate(R.layout.view_flipper_activity,container, false);
Spinner citizenship = (Spinner)v.findViewById(R.id.country_spinner);
citizenship.setAdapter(new CountriesListAdapter(getActivity(),recourseList));
And the countriesListAdapter is :
public class CountriesListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public CountriesListAdapter(Context context,String[] values){
super(context,R.layout.country_list_item,values);
this.context=context;
this.values=values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
TextView textView= (TextView) rowView.findViewById(R.id.txtViewCountryName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);
String[] g=values[position].split(",");
textView.setText(GetCountryZipCode(g[1]).trim());
String pngName = g[1].trim().toLowerCase();
imageView.setImageResource(context.getResources().getIdentifier("drawable/" + pngName, null, context.getPackageName()));
return rowView;
}
private String GetCountryZipCode(String ssid){
Locale loc = new Locale("", ssid);
return loc.getDisplayCountry().trim();
}
}
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<ImageView
android:id="@+id/imgViewFlag"
android:layout_height="40dp"
android:layout_width="40dp"
android:layout_margin="5dp" />
<TextView
android:id="@+id/txtViewCountryName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="hello"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/imgViewFlag" />
</RelativeLayout>
Logcat:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:896)
at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:892)
at android.widget.Spinner.measureContentWidth(Spinner.java:772)
at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1144)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:1173)
at android.widget.Spinner.performClick(Spinner.java:685)
at android.view.View$PerformClick.run(View.java:19311)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:896)