Android GridView getChildAt(0).findViewById()返回null [英] Android GridView getChildAt(0).findViewById() returns null

查看:59
本文介绍了Android GridView getChildAt(0).findViewById()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含某些项目的GridView,我使用了扩展BaseAdapter的自定义适配器.每行显示一个可下载项目,每行都有一个图标,显示下载进度.

I have a GridView with some items, I used a custom adapter that extends BaseAdapter. each row shows a downloadable item, each row has an icon that show download progress.

所以我需要更新此图标.

对于此更新,我知道我可以更改数据并调用notifyDataChanged(),视图将得到更新,但是这种方法使我付出了很多改变,而我不想这样做.

For this update, I know that I can change the data and call notifyDataChanged() and the view will get updated, but this approach costs me a lot of changes and I don't want to do this.

因此,我尝试从gridview中获取一个特定的孩子,找到该图标并更改该图标,但是返回的视图为空.

So I tried to get a specific child from the gridview, find this icon and change the icon, But the returned view is null.

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

更新2:

我的适配器实现 StickyGridHeadersBaseAdapter ,有时这会给我重复的id错误,所以我设置了mGridView.setId(-1);

My Adapter implements StickyGridHeadersBaseAdapter and sometimes this give me duplicate id error so i set mGridView.setId(-1);

推荐答案

您可以使用getItemIdAtPosition将ID与标头ID进行比较.如果不是标题,则应该是包含您的内容的视图.

You can use getItemIdAtPosition to compare the id against the id of headers. If it is not a header it should be the view containing your stuff.

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");
    }
}

他们在

They do this in the source of StickyGridHeadersGridView to find the headers, so it should work to find what is not the headers also.

这篇关于Android GridView getChildAt(0).findViewById()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆