2

I am implementing a List view using custom Layout. I am making an api call and the data is getting set into City.class object.
Actually I am getting the List populated with City object data. But when i put an OnListItemClick event on the List i am getting exception.

E/AndroidRuntime( 2072): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.stata.mobile.android.dao.City
E/AndroidRuntime( 2072):    at com.stata.mobile.android.ui.NearMeListFragment.onListItemClick(MyListFragment.java:108)
E/AndroidRuntime( 2072):    at com.stata.mobile.android.ui.NearMeListFragment$1.onItemClick(MyListFragment.java:78)
E/AndroidRuntime( 2072):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
E/AndroidRuntime( 2072):    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
E/AndroidRuntime( 2072):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
E/AndroidRuntime( 2072):    at android.widget.AbsListView$3.run(AbsListView.java:3638)
E/AndroidRuntime( 2072):    at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime( 2072):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 2072):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 2072):    at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime( 2072):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2072):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 2072):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime( 2072):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

My Fragment class code is like.

  public class MyListFragment  extends Fragment{
           @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.near_me, container,false);
            listView=(ListView)view.findViewById(R.id.list);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    onListItemClick((ListView) parent, view, position, id);
                }
            });

            new MyTask().execute();
            return view;
        }

    public void onListItemClick(ListView l, View v, int position, long id) {
        City city = ((City) l.getItemAtPosition(position));   // At this line i am getting the exception
        startActivity(new Intent(getActivity(), NearMeDetailActivity.class).putExtra("nearmecity",city));
    }
}

I am not able to figure out why it's happening. As soon as i am clicking on any item on the List i am getting this classCastException. I want to get that particular object and wants to show new activity where i can give the details of the city class

Adapter class

public class NearMeListAdapter extends BaseAdapter{
     private Activity activity;
    private List<City> data;
    private static LayoutInflater inflater=null;

 public MyListAdapter(Activity a, List<City> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

 public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

}
3
  • 2
    Problem is your Adapter class ... getItem method returns Integer instead City Commented Sep 23, 2014 at 13:22
  • @Selvin .. i have added the adapter class also . so might me i have made some mistake.. once please check and let me correct it Commented Sep 23, 2014 at 13:26
  • why don't you guys using ArrayAdapter<T> instead BaseAdapter? all you have to do is to override getView method ... instead activity inside ArrayAdapter you should use getContext() and to get item at getItem(at) ... to correct your code take a look at ArrayAdapter source ... Commented Sep 23, 2014 at 13:29

2 Answers 2

4

From the exception is clear that you have created an array of Integer. getItemAtPosition returns the element at position in your dataset. If you are adapter is made of City it will return an object of City.

Edit:

Your error is here:

  public Object getItem(int position) {
        return position;
  }

it should be

public Object getItem(int position) {
    return data.get(position);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have added the adapter class also .. can you figure what might have gone wrong
Thanx @blackbelt.. it got solved... thanx again for your quick reply
0

Declare it as a string then pass the intent.

String city = l.getItemAtPosition(position).toString();

Comments

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.