Android GridView getChildAt(0).findViewById() returns 空
Android GridView getChildAt(0).findViewById() returns null
我有一个 GridView
和一些项目,我使用了一个扩展 BaseAdapter
的自定义适配器。每行显示一个可下载的项目,每行都有一个显示下载进度的图标。
所以我需要更新这个图标。
对于这次更新,我知道我可以更改数据并调用 notifyDataChanged()
并且视图将得到更新,但这种方法花费了我很多更改,我不想这样做。
所以我试图从gridview
中获取一个特定的child,找到这个图标并更改图标,但是返回的视图是空的。
View v = mGridView.getChildAt(0);
if(v != null)
{
ImageButton mDownloadButton = (ImageButton)v.findViewById(R.id.download_icon_button);
if(mDownloadButton != null)
updateDownloadIcon(mDownloadButton, intent.getIntExtra(DATA.EXTRA_DOWNLOAD_PROGRESS, 0));
else
Log.e(TAG, "null image button");
}
v
不为空但 mDownloadButton
为空。
如有任何帮助,我们将不胜感激。
更新:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.all_book_frag_item_layout, null);
}
mBookCoverImageView = (ImageView)convertView.findViewById(R.id.bookCoverID);
mBookNameTextView = (TextView)convertView.findViewById(R.id.bookNameID);
mBookWriterTextView = (TextView)convertView.findViewById(R.id.bookWriterID);
mBookOtherInfoTextView = (TextView)convertView.findViewById(R.id.bookInfoID);
mAudioImageView = (ImageView)convertView.findViewById(R.id.audio_image);
mVideoImageView = (ImageView)convertView.findViewById(R.id.video_image);
mDownloadButton = (ImageButton)convertView.findViewById(R.id.download_icon_button);
mBookSize = (TextView)convertView.findViewById(R.id.download_size);
mBookNameInCover = (TextView)convertView.findViewById(R.id.bookNameInCover);
sectionRow = getSectionRow(position);
if(!mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmBookCover().equals(""))
{
imageLoader.DisplayImage(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmBookCover(), mBookCoverImageView);
mBookNameInCover.setText("");
}
else
{
mBookCoverImageView.setImageResource(R.drawable.trans_icon);
mBookNameInCover.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostTitle());
}
mBookNameTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostTitle());
mBookWriterTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmAuthorName());
mBookOtherInfoTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getCatString());
mBookSize.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmEpubSize());
if(isBookDownloaded(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostId()))
{
mDownloadButton.setImageResource(R.drawable.download_icon_90_100);
}
else
{
mDownloadButton.setImageResource(R.drawable.download_icon);
}
if(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).ismSound())
{
mAudioImageView.setVisibility(View.VISIBLE);
}
else
{
mAudioImageView.setVisibility(View.INVISIBLE);
}
if(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).ismVideo())
{
mVideoImageView.setVisibility(View.VISIBLE);
}
else
{
mVideoImageView.setVisibility(View.INVISIBLE);
}
if(mSelectsItems[sectionRow[0][0]][sectionRow[1][0]])
{
convertView.findViewById(R.id.allBookBaseLayout).setBackgroundResource(R.color.selection_color);
}
else
{
convertView.findViewById(R.id.allBookBaseLayout).setBackgroundResource(R.drawable.book_view_one_column_item_background);
}
mDownloadButton.setOnClickListener(new DownloadClickListener(mDownloadButton, mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]), position));
return convertView;
}
更新二:
我的适配器实现 StickyGridHeadersBaseAdapter,有时这会给我重复 ID 错误,所以我设置 mGridView.setId(-1);
您似乎使用了错误的 ID,您确定 R.id.download_icon_button 在视图中吗?如果没有,您必须添加它。
您可以使用 getItemIdAtPosition
将 id 与 header 的 id 进行比较。如果它不是 header,它应该是包含您的内容的视图。
long id = mGridView.getItemIdAtPosition(i);
if (id != StickyGridHeadersBaseAdapterWrapper.ID_HEADER) {
// you now know that this item is not a header.
View v = mGridView.getChildAt(i);
if(v != null)
{
ImageButton mDownloadButton = (ImageButton)v.findViewById(R.id.download_icon_button);
if(mDownloadButton != null)
updateDownloadIcon(mDownloadButton, intent.getIntExtra(DATA.EXTRA_DOWNLOAD_PROGRESS, 0));
else
Log.e(TAG, "null image button");
}
}
他们在 StickyGridHeadersGridView 的 source 中执行此操作以找到 headers,因此它应该也能找到不是 headers 的东西。
我有一个 GridView
和一些项目,我使用了一个扩展 BaseAdapter
的自定义适配器。每行显示一个可下载的项目,每行都有一个显示下载进度的图标。
所以我需要更新这个图标。
对于这次更新,我知道我可以更改数据并调用 notifyDataChanged()
并且视图将得到更新,但这种方法花费了我很多更改,我不想这样做。
所以我试图从gridview
中获取一个特定的child,找到这个图标并更改图标,但是返回的视图是空的。
View v = mGridView.getChildAt(0);
if(v != null)
{
ImageButton mDownloadButton = (ImageButton)v.findViewById(R.id.download_icon_button);
if(mDownloadButton != null)
updateDownloadIcon(mDownloadButton, intent.getIntExtra(DATA.EXTRA_DOWNLOAD_PROGRESS, 0));
else
Log.e(TAG, "null image button");
}
v
不为空但 mDownloadButton
为空。
如有任何帮助,我们将不胜感激。
更新:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.all_book_frag_item_layout, null);
}
mBookCoverImageView = (ImageView)convertView.findViewById(R.id.bookCoverID);
mBookNameTextView = (TextView)convertView.findViewById(R.id.bookNameID);
mBookWriterTextView = (TextView)convertView.findViewById(R.id.bookWriterID);
mBookOtherInfoTextView = (TextView)convertView.findViewById(R.id.bookInfoID);
mAudioImageView = (ImageView)convertView.findViewById(R.id.audio_image);
mVideoImageView = (ImageView)convertView.findViewById(R.id.video_image);
mDownloadButton = (ImageButton)convertView.findViewById(R.id.download_icon_button);
mBookSize = (TextView)convertView.findViewById(R.id.download_size);
mBookNameInCover = (TextView)convertView.findViewById(R.id.bookNameInCover);
sectionRow = getSectionRow(position);
if(!mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmBookCover().equals(""))
{
imageLoader.DisplayImage(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmBookCover(), mBookCoverImageView);
mBookNameInCover.setText("");
}
else
{
mBookCoverImageView.setImageResource(R.drawable.trans_icon);
mBookNameInCover.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostTitle());
}
mBookNameTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostTitle());
mBookWriterTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmAuthorName());
mBookOtherInfoTextView.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getCatString());
mBookSize.setText(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmEpubSize());
if(isBookDownloaded(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).getmPostId()))
{
mDownloadButton.setImageResource(R.drawable.download_icon_90_100);
}
else
{
mDownloadButton.setImageResource(R.drawable.download_icon);
}
if(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).ismSound())
{
mAudioImageView.setVisibility(View.VISIBLE);
}
else
{
mAudioImageView.setVisibility(View.INVISIBLE);
}
if(mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]).ismVideo())
{
mVideoImageView.setVisibility(View.VISIBLE);
}
else
{
mVideoImageView.setVisibility(View.INVISIBLE);
}
if(mSelectsItems[sectionRow[0][0]][sectionRow[1][0]])
{
convertView.findViewById(R.id.allBookBaseLayout).setBackgroundResource(R.color.selection_color);
}
else
{
convertView.findViewById(R.id.allBookBaseLayout).setBackgroundResource(R.drawable.book_view_one_column_item_background);
}
mDownloadButton.setOnClickListener(new DownloadClickListener(mDownloadButton, mHeaderInfo.get(sectionRow[0][0]).getmBooksInCat().get(sectionRow[1][0]), position));
return convertView;
}
更新二:
我的适配器实现 StickyGridHeadersBaseAdapter,有时这会给我重复 ID 错误,所以我设置 mGridView.setId(-1);
您似乎使用了错误的 ID,您确定 R.id.download_icon_button 在视图中吗?如果没有,您必须添加它。
您可以使用 getItemIdAtPosition
将 id 与 header 的 id 进行比较。如果它不是 header,它应该是包含您的内容的视图。
long id = mGridView.getItemIdAtPosition(i);
if (id != StickyGridHeadersBaseAdapterWrapper.ID_HEADER) {
// you now know that this item is not a header.
View v = mGridView.getChildAt(i);
if(v != null)
{
ImageButton mDownloadButton = (ImageButton)v.findViewById(R.id.download_icon_button);
if(mDownloadButton != null)
updateDownloadIcon(mDownloadButton, intent.getIntExtra(DATA.EXTRA_DOWNLOAD_PROGRESS, 0));
else
Log.e(TAG, "null image button");
}
}
他们在 StickyGridHeadersGridView 的 source 中执行此操作以找到 headers,因此它应该也能找到不是 headers 的东西。