silverFoxA
silverFoxA

Reputation: 4659

textview casting error:- android.widget.LinearLayout cannot be cast to android.widget.TextView

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)

       

Upvotes: 0

Views: 2102

Answers (2)

electrocrat
electrocrat

Reputation: 464

Try this in the constructor for CountriesListAdapter class:

super(context, R.layout.country_list_item, R.id.txtViewCountryName, values);

instead of:

super(context,R.layout.country_list_item,values);

I just tried it and it worked.

Edit: I also had to modify your layout as the TextView was interfering with the ImageView:

<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_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/txtViewCountryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/imgViewFlag"
        android:layout_toRightOf="@id/imgViewFlag"
        android:text="hello"
        android:textAppearance="@android:style/TextAppearance.Medium.Inverse" />

</RelativeLayout>

And the reason why the country images were not showing up in the selector list was because you also have to override getDropDownView in CountriesListAdapter and do the same thing that is being done in getView like so:

@Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

I tried it with:

values = new String[]{ "CHINA,zh_CN", "JAPAN,ja_JP", "KOREA,ko_KR", "TAIWAN,zh_TW" }

and drawable resource images zh_cn.png, ja_jp.png, ko_kr.png and zh_tw.png

Upvotes: 4

Arshid KV
Arshid KV

Reputation: 10037

Change code String[] g=values[position].split(","); textView.setText(GetCountryZipCode(g[1]).trim());

To

String[] g=values[position].split(","); String k= g[1]; textView.setText((GetCountryZipCode[k]).trim());

Upvotes: 0

Related Questions