Android: Custom ListAdapter extending BaseAdapter crashes on application launch
Asked Answered
B

2

19

Data being pulled from a local DB, then mapped using a cursor. Custom Adapter displays data similar to a ListView. As items are added/deleted from the DB, the adapter is supposed to refresh. The solution attempted below crashes the application at launch. Any suggestions?

Thanks in advance, -D

 @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;
 }

Exception:

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)
...
Botulinus answered 23/3, 2010 at 0:21 Comment(0)
A
87
v = vi.inflate(R.layout.items_row, p);

Add a false third parameter to that call, and I think your problem will go away. The call should become:

v = vi.inflate(R.layout.items_row, p, false);
Armet answered 23/3, 2010 at 0:43 Comment(1)
This is the answer! I had this issue already but I'd forgotten to put in the extra false parameter this time since it's been a while and sure enough this fixed it right up.Egyptian
A
7

change this code

v = vi.inflate(R.layout.items_row, p);

to

v = vi.inflate(R.layout.items_row, null );
Anniceannie answered 28/12, 2010 at 11:30 Comment(1)
warning! Wrong answer. Please see doubleencore.com/2013/05/layout-inflation-as-intendedDey

© 2022 - 2024 — McMap. All rights reserved.