我有两个数据库模型,一个是测试模型,另一个是图像模型 我将图像作为字节存储在我的数据库中 当我从数据库中检索字节时,我收到此错误。
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: java.lang.ClassCastException: upload.syncdatabase.TestModel cannot be cast to upload.syncdatabase.ImageModel
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at upload.syncdatabase.MainActivity$MyAdapter.getView(MainActivity.java:230)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2344)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1182)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:582)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:649)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.View.measure(View.java:17430)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-17 02:17:21.143 22974-22974/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
在我的适配器中,我调用testmodel和imagemodel。因为存储在imagetable中的图像和其他信息是在测试模型中。
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<TestModel> mDatas;
private List<ImageModel> xDatas;
public MyAdapter(@NonNull Context context, @Nullable List<TestModel> datas,@Nullable List<ImageModel> pDatas) {
super();
mContext = context;
mDatas = datas;
xDatas=pDatas;
}
public void swapItems(@NonNull final List<TestModel> datas) {
mDatas = datas;
notifyDataSetChanged();
}
@Override
public int getCount() {
if (mDatas == null) {
return 0;
}
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView txtName;
TextView txtNumber;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtNumber = (TextView) convertView.findViewById(R.id.txtNumber);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
TestModel data = (TestModel) getItem(position);
holder.txtName.setText(data.POI);
holder.txtNumber.setText(String.valueOf(data.POIAddress));
ImageModel dataM = (ImageModel) getItem(0);//getting errors here
return convertView;
}
}
答案 0 :(得分:1)
添加Adapter
public ImageModel getItem2(int position) { return xDatas.get(position); }
并在ImageModel
getView(....)
个数据
ImageModel dataM = (ImageModel) getItem2(0);