我想在我的应用中使用片段,这些片段将针对平板电脑。我试图在屏幕左侧显示一个由SimpleCursorAdapter支持的列表视图。
我知道我应该使用一个Loader,因为它不能在UI线程上运行,但应用程序也会针对手机,这可能是API 9。
我设置了一个Activity来容纳2个片段,左边是一个列表,右边是一个细节片段。
首先,我只想简单地在列表视图中显示光标中的数据,稍后我会将其附加到活动中,并使活动实现一个监听器等。我是碎片的新手,所以只想先显示数据。
我在SimpleCursorAdapter上遇到ClassCastException,有没有人有任何想法?
如果我以错误的方式这样做,请注意,但任何帮助都会受到赞赏。
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>
答案 0 :(得分:1)
myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
你不能这样做。而是改变它,如下面
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
或者您已经在SimpleCursorAdapter
中扩展了MyAdapter
来创建它的实例。
myAdapter = new MyAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
答案 1 :(得分:0)
无需编写SimpleCursorAdapter
,因为您已在MyAdapter
课程中实施了它。因此,您只需要创建MyAdapter
类的实例。
试试如下:
更改以下内容
myAdapter = (MyAdapter) new SimpleCursorAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
如下:
myAdapter = new MyAdapter(getActivity(),R.layout.carerrow , cursor, from, to);
答案 2 :(得分:0)
myAdapter =(MyAdapter)new SimpleCursorAdapter(getActivity(),R.layout.carerrow,cursor,from, 到);
只需将SimpleCursorAdapter
替换为MyAdapter
...
此外,您可以使用支持v4库来获取预蜂窝设备上的加载器。