0

I am running the following code onItemClickListener of a ListView. But it shows ClassCastException when i put the string at position arg2 in a string s as:

String s= (String) arg0.getItemAtPosition(arg2);

Code snippet:

lv1.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
       String out;
       String s = (String) arg0.getItemAtPosition(arg2);
       //some code
     }
}

I am posting the stack trace also :

07-14 02:35:42.798: E/AndroidRuntime(412): FATAL EXCEPTION: main
07-14 02:35:42.798: E/AndroidRuntime(412): java.lang.ClassCastException: bitcream.whats.your.score.Rest
07-14 02:35:42.798: E/AndroidRuntime(412):  at bitcream.whats.your.score.Intermediate$1.onItemClick(Intermediate.java:55)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.ListView.performItemClick(ListView.java:3513)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Handler.handleCallback(Handler.java:587)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Looper.loop(Looper.java:123)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 02:35:42.798: E/AndroidRuntime(412):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 02:35:42.798: E/AndroidRuntime(412):  at java.lang.reflect.Method.invoke(Method.java:507)
07-14 02:35:42.798: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 02:35:42.798: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 02:35:42.798: E/AndroidRuntime(412):  at dalvik.system.NativeStart.main(Native Method)
07-14 02:35:45.468: I/Process(412): Sending signal. PID: 412 SIG: 9

Please suggest a fix.

The adapter code is :

public class Myadapter extends ArrayAdapter<Rest>{

    Context ctx;
    List<Rest> list;
    Rest r1;
    ImageView iv1;
    TextView tv1, tv2;
    public Myadapter(Context context, int textViewResourceId,
            List<Rest> objects) {
        super(context, textViewResourceId, objects);
        ctx = context;
        list = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater lii = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View row = lii.inflate(R.layout.li, null, false);
        tv1 = (TextView) row.findViewById(R.id.textView1);
        r1 = list.get(position);
        tv1.setText(r1.name);
        work();
        return row;
    }

The Rest class is as:

package bitcream.whats.your.score;

public class Rest {
    String name;
    public Rest(String name) {
        this.name = name;
    }
}
4
  • 1
    Does the statement arg0.getItemAtPosition(arg2) returns String? If not convert them to String first before storing them to String s. Commented Jul 14, 2014 at 3:40
  • Post the adapter code Commented Jul 14, 2014 at 3:42
  • post the Rest Class code. DO you have getter and setter's there. also post the adapter code and look at developer.android.com/reference/android/widget/… Commented Jul 14, 2014 at 3:46
  • I have posted it @Raghunandan Commented Jul 14, 2014 at 3:49

2 Answers 2

3

Your list is populated by Rest. public class Myadapter extends ArrayAdapter<Rest>{

public Object getItemAtPosition (int position)

Added in API level 1
Gets the data associated with the specified position in the list.

Parameters
position    Which data to get
Returns
The data associated with the specified position in the list

So this arg0.getItemAtPosition(arg2) does not return a String and you cast it to String leading to ClassCastException.

So in onItemClick

Rest rs = (Rest) arg0.getItemAtPosition(arg2);
String value= rs.name
Sign up to request clarification or add additional context in comments.

Comments

1

Your type isn't a String, but it could be any Object (because of the ? type, in this case it happened to be a bitcream.whats.your.score.Rest). I believe you want to use toString()

String s = arg0.getItemAtPosition(arg2).toString();

Edit

Based on your update, you should add toString() to Rest - something like,

public class Rest {
  String name;
  public Rest(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return name;
  }
}

4 Comments

arg0.getItemAtPosition(arg2) it itself return a String so if i cast it to String whats wrong in it the tostring() function also does the same i guess
If this String s= (String) arg0.getItemAtPosition(arg2); gives you a ClassCastException then it is not a String.
Gets the data associated with the specified position in the list. developer.android.com/reference/android/widget/…
@Shubhankar Yes, and it returns an Object - not a String. public Object getItemAtPosition (int position)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.