Trying to get the _id when an item in gridview is clicked getting a cast exception

94 Views Asked by At

I am trying to get the _id field from the cursor when an item in my Grid View is clicked, the same code is working fine in a listview but does not seem to work here:

Class level:

CurAdapter Cur;
GridView grid;

onCreate:

Cursor mCur = dbHelper.contactsQuery(); 
grid = (GridView)rootView.findViewById(R.id.gridview); 
Cur = new CurAdapter(getActivity(), mCur,0); 
grid.setAdapter(Cur);
grid.setTextFilterEnabled(true);

onCLick:

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

                System.out.println(position);
                System.out.println(id);

                Cursor cursor = (Cursor) grid.getItemAtPosition(position);

            }
        }); 

My CustomAdapter:

private class CurAdapter extends CursorAdapter{

        public CurAdapter(Context context, Cursor c, int flags) {

            super(context, c, flags);

        }



        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView tv = (TextView) view.findViewById(R.id.item_text);
            tv.setTypeface(helv_light); 
            final RoundedImageView iv = (RoundedImageView) view.findViewById(R.id.item_image);

            String name = (cursor.getString(cursor.getColumnIndexOrThrow("Name")));
            image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
            String isAdhoc = (cursor.getString(cursor.getColumnIndexOrThrow("isAdhoc")));

            if(isAdhoc.equals("1")){
                boolean check = false; 
                String frName = null;
                Cursor mCur = dbHelper.friendsName(name);
                if(mCur != null && mCur.moveToFirst()){
                    do{
                        frName = mCur.getString(mCur.getColumnIndex("FriendsName")); 
                        if(frName != null){


                            if(frName.equalsIgnoreCase(selfName)){
                                check = false; 
                            }else {

                                check = true; 


                            }  


                        }

                    }while (mCur.moveToNext()); 
                }

                if(check){
                    name = name+" "+"("+frName+"'s"+" pet)"; 
                }

            }


            tv.setText(name);


            if((image.contains("jpg") || image.contains("png")) && image.contains("adhoc") != true){

                image = "file://"+image; 

            }

            final DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.icon_default)
            .showImageForEmptyUri(R.drawable.icon_default)
            .showImageOnFail(R.drawable.icon_default) 
            .resetViewBeforeLoading(true)  
            .cacheInMemory(true) // default
            .cacheOnDisk(true) // default
            .build();

            ImageLoader.getInstance().displayImage(image, iv, options); 

        } 
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = LayoutInflater.from(context).inflate(R.layout.grid_layout, null);

            return view;

        }


        public long getItemId(int position) { 

            return position;  
        }  

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

    }

My Problem:

11-10 14:51:47.989: E/AndroidRuntime(11194): java.lang.ClassCastException: java.lang.Integer cannot be cast to android.database.Cursor
11-10 14:51:47.989: E/AndroidRuntime(11194):    at com.example.FragmentTab3$1.onItemClick(FragmentTab3.java:125)
2

There are 2 best solutions below

6
On BEST ANSWER

you are getting the ClassCastException because you are overriding getItem

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

and in your case it is returning an int not the cursor. You can delete it and use the parent implementation of getItem (which should return the Cursor at position)

0
On

Exception is due to return int from getItem(..) as mentioned by blackbelt

This may also help you to get ID of the record - change getItemId(..) this way:

public long getItemId(int position) { 
            mCur.moveToPosition(position);
            return mCur.getLong(mCur.getColumnIndex(BaseColumns._ID));  
        }