我是java和android编程的新手。所以我的项目是一次巨大的学习经历。我正在使用一个cursorloader从数据库中检索数据,然后将该数据分配给自定义适配器。我想要做的是获取数据并根据数据是什么,将其分配给某个行布局。
将有大约6-12种不同的布局。数据包含不同类型的设备,例如灯,调光器,恒温器等。每种类型的设备都与类别编号相关联。灯的类别为3,调光器的类别编号为2.我想检查该类别,并根据编号将其分配给某个行布局。怎么会这样做呢?我正在尝试创建一个自定义simplecursoradapter。这是我到目前为止所发生的事情,但它崩溃了。
public class DeviceAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private ViewHolder holder;
private LightViewHolder lightholder;
private TempViewHolder tempholder;
public DeviceAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (cursor.getString(4).equals("17")) {;
tempholder = (TempViewHolder)view.getTag();
tempholder.name.setText(cursor.getString(1));
tempholder.temp.setText(cursor.getString(10));
tempholder.tempImage.setImageResource(R.drawable.thermometer);
} else if (cursor.getString(4).equals("3")) {
lightholder = (LightViewHolder)view.getTag();
lightholder.name.setText(cursor.getString(1));
lightholder.status.setText(cursor.getString(10));
lightholder.lightImage.setImageResource(R.drawable.light_on);
} else {
holder = (ViewHolder)view.getTag();
holder.name.setText(cursor.getString(1));
holder.status.setText(cursor.getString(2));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
if (cursor.getString(4).equals("17")) {
View view = mInflater.inflate(R.layout.temperature_device_list_item, null);
tempholder = new TempViewHolder();
tempholder.name = (TextView)view.findViewById(R.id.devicename);
tempholder.temp = (TextView)view.findViewById(R.id.temp);
tempholder.tempImage = (ImageView)view.findViewById(R.id.tempImage);
view.setTag(tempholder);
return view;
} else if (cursor.getString(4).equals("3")) {
View view = mInflater.inflate(R.layout.light_list_item, null);
lightholder = new LightViewHolder();
lightholder.name = (TextView)view.findViewById(R.id.devicename);
lightholder.status = (TextView)view.findViewById(R.id.status);
lightholder.lightImage = (ImageView)view.findViewById(R.id.deviceImage);
view.setTag(lightholder);
return view;
} else {
View view = mInflater.inflate(R.layout.device_list_item, null);
holder = new ViewHolder();
holder.name = (TextView)view.findViewById(R.id.devicename);
holder.status = (TextView)view.findViewById(R.id.status);
view.setTag(holder);
return view;
}
}
private static class LightViewHolder {
TextView name;
TextView status;
ImageView lightImage;
}
private static class TempViewHolder {
TextView name;
TextView temp;
ImageView tempImage;
}
private static class ViewHolder {
TextView name;
TextView status;
}
}
这看起来不错吗?记住,我是所有这一切的新手,并且很难找到一些好的例子。大多数代码示例都在调用getView,我读到你应该使用bindView和newView。
我的应用程序有一个房间片段和一个包含设备列表的片段。单击一个房间时会发生崩溃,列表将显示设备。如果我去点击另一个房间,应用程序将崩溃,我得到以下内容:
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): FATAL EXCEPTION: main
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): java.lang.ClassCastException: com.garrettpower.powerha.listadapters.listview.DeviceAdapter$LightViewHolder cannot be cast to com.garrettpower.powerha.listadapters.listview.DeviceAdapter$ViewHolder
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at com.garrettpower.powerha.listadapters.listview.DeviceAdapter.bindView(DeviceAdapter.java:42)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.CursorAdapter.getView(CursorAdapter.java:251)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.AbsListView.obtainView(AbsListView.java:1949)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.ListView.makeAndAddView(ListView.java:1756)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.ListView.fillSpecific(ListView.java:1302)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.ListView.layoutChildren(ListView.java:1587)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.AbsListView.onLayout(AbsListView.java:1800)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.widget.FrameLayout.onLayout(FrameLayout.java:400)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.View.layout(View.java:9588)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewGroup.layout(ViewGroup.java:3877)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewRoot.performTraversals(ViewRoot.java:1288)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.view.ViewRoot.handleMessage(ViewRoot.java:2046)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.os.Looper.loop(Looper.java:132)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at java.lang.reflect.Method.invoke(Method.java:491)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-17 03:41:07.033: ERROR/AndroidRuntime(7210): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我知道这已经很老了,但万一有人从谷歌或其他地方遇到过这种情况。
问题是bindView
试图将TempViewHolder
强制转换为ViewHolder
- 这两个都是在适配器中定义的自定义内部类,并且因为它们是不同的,所以它们不能是从一个人转到另一个人。
永远假设非常重要ListView
在convertView
中为您提供与您需要的视图类型相同的视图类型。
至少这是堆栈跟踪显示的内容。可能还有其他问题,但修复将揭示下一个问题。