我正在使用 BaseAdapter 来显示自定义的 ListView-Items。此代码工作正常:
public class BA_OneNoneHandler extends BaseAdapter {
private Context coContext;
private Cursor cuCursor;
public BA_OneNoneHandler(Context context, Cursor cur) {
super();
coContext = context;
cuCursor = cur;
}
public View getView(int iPosition, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) coContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.termin_list_item, null);
cuCursor.moveToPosition(iPosition);
((TextView) view.findViewById(R.id.TerminListItem_tvHeadline)).setText("Hallo");
return view;
}
public int getCount() {
// return the number of records in cursor
return cuCursor.getCount();
}
public Object getItem(int iPosition) {
// TODO Auto-generated method stub
return iPosition;
}
public long getItemId(int iPosition) {
// TODO Auto-generated method stub
return iPosition;
}
}
...但是如果我想使用 ViewHolder,如果 ListView 向下滚动,应用程序就会崩溃。
public class BA_OneHandler extends BaseAdapter {
private Context coContext;
private Cursor cuCursor;
public BA_OneHandler(Context context, Cursor cur) {
super();
coContext = context;
cuCursor = cur;
}
public View getView(int iPosition, View view, ViewGroup parent) {
ViewHolder vhViewHolder;
if( view == null){
vhViewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) coContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.termin_list_item, null);
// XML-element
vhViewHolder.tvHeadLine = (TextView) view.findViewById(R.id.TerminListItem_tvHeadline);
} else {
vhViewHolder = (ViewHolder)view.getTag();
}
cuCursor.moveToPosition(iPosition);
vhViewHolder.tvHeadLine.setText("Hallo");
return view;
}
private static class ViewHolder{
// XML-elements from "termin_list_adapter.xml"
protected TextView tvHeadLine;
}
public int getCount() {
// return the number of records in cursor
return cuCursor.getCount();
}
public Object getItem(int iPosition) {
// TODO Auto-generated method stub
return iPosition;
}
public long getItemId(int iPosition) {
// TODO Auto-generated method stub
return iPosition;
}
}
我发现了类似的 Questios,但它们都在处理 JSON。我的只是一个简单的列表。这是堆栈...
java.lang.NullPointerException
at com.progstar.android.contermine.BA_OneHandler.getView(BA_OneHandler.java:51)
at android.widget.AbsListView.obtainView(AbsListView.java:2143)
at android.widget.ListView.makeAndAddView(ListView.java:1831)
at android.widget.ListView.fillDown(ListView.java:674)
at android.widget.ListView.fillGap(ListView.java:638)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
at android.view.View.dispatchTouchEvent(View.java:7239)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
at android.view.View.dispatchPointerEvent(View.java:7419)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:530)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5226)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
第 51 行)->
vhViewHolder.tvHeadLine.setText("Hallo");
我正在使用 BaseAdapter,因为我是 android 新手,看到了数百个适配器并遵循了一个,这似乎以最好的方式显示我的自定义列表。我什至用谷歌搜索了很多“哪个适配器适用于哪个应用程序”,但没有令人满意的概述。所以我从代码示例中尝试了一个,而不是用太多的初学者问题来打扰社区。选择 BaseAdapter 不是一个好主意吗?
另一方面,SimpeCursorAdapter 是 > API 11(我也想支持 Android 2.3)并且 CursorAdapter 是抽象的,所以我不得不四处游荡。