Logcat error: “addView(View, LayoutParams) is not supported in AdapterView” in a ListView

前端 未结 5 1277
误落风尘
误落风尘 2020-12-01 10:22

I\'m doing an application for Android and something I need is that it shows a list of all files and directories in the SD Card and it has to be able to move through the diff

相关标签:
5条回答
  • 2020-12-01 10:38

    Delete TextView From ListView in your XML file

    0 讨论(0)
  • 2020-12-01 10:46

    When creating an ArrayAdapter like yours, the ArrayAdapter needs a layout resource containing just a TextView. Try changing your ArrayAdapter creation with:

    setListAdapter(new ArrayAdapter<String>(this, 
          android.R.layout.simple_list_item_1, 
          this.directoryEntries));
    

    and see if it works.

    0 讨论(0)
  • 2020-12-01 10:56

    For others who have this problem and have inflated a layout in ArrayAdapter's getView, set the parent parameter to null, as in view = inflater.inflate(R.layout.mylayout, null);

    0 讨论(0)
  • 2020-12-01 10:56

    my problem was this: I override getView in arrayAdapter. then when I inflated view, I forgot to set attach to root to false and that caused this exception.

    this is how must be done:

    convertView = LayoutInflater.from(getContext()).inflate(R.layout.market_search_product_view, parent,false);
    

    after that problem solved!

    0 讨论(0)
  • Rule of thumb

    You should always try to pass in a parent while inflating a View, else the framework will not be able to identify what layout params to use for that inflated view.

    The correct way of handling the exception is by passing false as third parameter, this flag indicates if a view should be added directly to the passed container or not.

    In the case of an adapter, the adapter itself will add the view after you returned the view from you getview method:

    view = inflater.inflate(R.layout.mylayout, parent, false);

    BaseAdapter sample, comes from just-for-us DevBytes:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View result = convertView;
            if (result == null) {
                result = mInflater.inflate(R.layout.grid_item, parent, false);
            }
    
            // Try to get view cache or create a new one if needed
            ViewCache viewCache = (ViewCache) result.getTag();
            if (viewCache == null) {
                viewCache = new ViewCache(result);
                result.setTag(viewCache);
            }
    
            // Fetch item
            Coupon coupon = getItem(position);
    
            // Bind the data
            viewCache.mTitleView.setText(coupon.mTitle);
            viewCache.mSubtitleView.setText(coupon.mSubtitle);
            viewCache.mImageView.setImageURI(coupon.mImageUri);
    
            return result;
        }
    

    CursorAdapter sample:

        @Override
        public View newView(final Context context, final Cursor cursor, final ViewGroup root)          {
            return mInflater.inflate(R.layout.list_item_ticket, root, false);
        }
    

    More information about view inflation can be found in this article.

    0 讨论(0)
提交回复
热议问题