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
Delete TextView From ListView in your XML file
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.
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);
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!
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.