gpt4 book ai didi

android - fragment 中的 SimpleCursorAdapter ClassCastException

转载 作者:行者123 更新时间:2023-11-30 03:07:13 24 4
gpt4 key购买 nike

我想在我的应用中使用针对平板电脑的 fragment 。我正在尝试在屏幕左侧显示一个由 SimpleCursorAdapter 支持的 ListView 。

我知道我可能应该使用加载器,因为它不在 UI 线程上运行,但该应用程序也将针对手机,可能是 API 9。

我设置了一个 Activity 来容纳 2 个 fragment ,左边一个列表,右边一个细节 fragment 。

首先,我只想简单地在 ListView 中显示来自光标的数据,稍后我会将其附加到 Activity 中,并让 Activity 实现监听器等。我是 fragment 的新手,所以只想先显示数据。

我在 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>

最佳答案

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

你不能这样做。而是像下面这样改变它

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

或者因为您已经在 MyAdapter 中扩展了 SimpleCursorAdapter 创建它的一个实例。

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

关于android - fragment 中的 SimpleCursorAdapter ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21599808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com