Question

I want to use fragments in my app that will be targeted at tablets. I am trying to show a listview to the left of the screen which is backed by a SimpleCursorAdapter.

I know i should probably use a Loader as it doesn't run on UI thread but the app will target phones as well which could be API 9.

I've setup an Activity to hold 2 fragments, the left a list, the right one a details Fragment.

Firstly i just want to simply show the data from the cursor in the listview, i'll attach it to the activity later and make the activity implement a listener etc later. I'm new to fragments so just want to show the data first.

I'm getting a ClassCastException on the SimpleCursorAdapter, Has anyone any ideas why?

Appologies if i'm doing this the wrong way, but any help would be appreciated.

02-06 09:59:07.718: E/AndroidRuntime(1414): FATAL EXCEPTION: main
02-06 09:59:07.718: E/AndroidRuntime(1414): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carefreegroup.rr3.carefreeoncall/com.carefreegroup.rr3.carefreeoncall.OnCallListAndDetailsActivity}: java.lang.ClassCastException: android.widget.SimpleCursorAdapter cannot be cast to com.carefreegroup.rr3.carefreeoncall.CarerListFragment$MyAdapter
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.os.Looper.loop(Looper.java:126)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread.main(ActivityThread.java:3997)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at java.lang.reflect.Method.invoke(Method.java:491)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at dalvik.system.NativeStart.main(Native Method)
02-06 09:59:07.718: E/AndroidRuntime(1414): Caused by: java.lang.ClassCastException: android.widget.SimpleCursorAdapter cannot be cast to com.carefreegroup.rr3.carefreeoncall.CarerListFragment$MyAdapter
02-06 09:59:07.718: E/AndroidRuntime(1414):     at com.carefreegroup.rr3.carefreeoncall.CarerListFragment.onActivityCreated(CarerListFragment.java:58)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:749)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1579)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.Activity.performStart(Activity.java:4326)
02-06 09:59:07.718: E/AndroidRuntime(1414):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
02-06 09:59:07.718: E/AndroidRuntime(1414):     ... 11 more

.

import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public  class CarerListFragment extends Fragment {

    private static final String TAG = CarerListFragment.class.getSimpleName();
    RROnCallApplication rrOnCallApp;
    Cursor cursor;
    ListView listView;
    MyAdapter myAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rrOnCallApp = (RROnCallApplication) getActivity().getApplicationContext();
        cursor = rrOnCallApp.dbModel.queryAllFromCarer();

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragmentcarerlistlayout, container, false);
    }






    @SuppressWarnings("deprecation")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String[] from = new String[]{DBModel.C_CARER_FIRSTNAME, DBModel.C_CARER_LASTNAME, DBModel.C_CARER_PHONENUMBER};
        int[] to = {R.id.carerrowfirstname, R.id.carerrowlastname, R.id.carerrowtelno};


        myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
        listView = (ListView) getActivity().findViewById(R.id.list);
        listView.setAdapter(myAdapter);
    }






    private class MyAdapter extends SimpleCursorAdapter {

        @SuppressWarnings("deprecation")
        public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);


        }

        @Override
        public
        View getView(int position, View convertView, ViewGroup parent) {
            Log.e(TAG, "inside myadapter getview");
            View v = super.getView(position, convertView, parent);
            if(v == null)
                return null;

            Cursor c = (Cursor)getItem(position);
           // String tagScanTime = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));

           // ((TextView)v.findViewById(R.id.rowcarername)).setText(name + " signed " + status +" at ");

            return v;
        }
    }

}//end of CarerListFragment

.

carerrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     >

    <TextView
        android:id="@+id/carerrowfirstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/carerrowlastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
     <TextView
        android:id="@+id/carerrowtelno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />



</LinearLayout>

.

fragmentcarerlistlayout.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#DEDEDE"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>


 </LinearLayout>
Was it helpful?

Solution 2

myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);

Just replace SimpleCursorAdapter with MyAdapter...

Also, you could use support v4 library to get loaders on pre-honeycomb devices.

OTHER TIPS

myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);

You can't do this. Instead change it like below

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);

or since you already extended SimpleCursorAdapter in MyAdapter create an instance of it.

myAdapter = new MyAdapter(getActivity(),R.layout.carerrow , cursor, from, to);

There is no need to write SimpleCursorAdapter as you have already implemented it in your MyAdapter class. So just you need to create an instance of MyAdapter class only.

Try out as below:

Change your below line

 myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);

To as below:

myAdapter = new MyAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top