Android:自定义ListAdapter扩展BaseAdapter在应用程序启动时崩溃

Dan*_*nny 18 android

从本地数据库中提取数据,然后使用游标进行映射.自定义适配器显示类似于ListView的数据.在从DB添加/删除项目时,应该刷新适配器.下面尝试的解决方案在启动时崩溃了应用程序.有什么建议?

提前致谢

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   View v = convertView;
   ViewGroup p = parent;            
   if (v == null) {
     LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     v = vi.inflate(R.layout.items_row, p);
   }
   int size = mAdapter.getCount();
   Log.d(TAG, "position " + position + " Size " + size);
   if(size != 0){
     if(position < size) return mAdapter.getView(position, v, p);
     Log.d(TAG, "-position " + position + " Size " + size);
   }
   return null;
 }
Run Code Online (Sandbox Code Playgroud)

例外:

03-23 00:14:10.392: ERROR/AndroidRuntime(718): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
03-23 00:14:10.392: ERROR/AndroidRuntime(718):     at android.widget.AdapterView.addView(AdapterView.java:461)
03-23 00:14:10.392: ERROR/AndroidRuntime(718):     at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
03-23 00:14:10.392: ERROR/AndroidRuntime(718):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-23 00:14:10.392: ERROR/AndroidRuntime(718):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-23 00:14:10.392: ERROR/AndroidRuntime(718):     at com.xyz.abc.CustomSeparatedListAdapter.getView(CustomSeparatedListAdapter.java:90)
...
Run Code Online (Sandbox Code Playgroud)

Com*_*are 87

v = vi.inflate(R.layout.items_row, p);
Run Code Online (Sandbox Code Playgroud)

false在该调用中添加第三个参数,我认为您的问题将会消失.电话应成为:

v = vi.inflate(R.layout.items_row, p, false);
Run Code Online (Sandbox Code Playgroud)

  • 一年后,这个评论我仍然有用!谢谢^^ (6认同)

Aru*_*r S 7

更改此代码

v = vi.inflate(R.layout.items_row, p);
Run Code Online (Sandbox Code Playgroud)

v = vi.inflate(R.layout.items_row, null );
Run Code Online (Sandbox Code Playgroud)