2015-06-20 197 views
1

我有一個GridView與一些項目,我用一個自定義適配器,延伸BaseAdapter。每行顯示可下載的項目,每行都有一個顯示下載進度的圖標。Android GridView getChildAt(0).findViewById()返回null

所以我需要更新這個圖標。

對於此次更新,我知道我可以更改數據並致電notifyDataChanged(),並且視圖將會更新,但是這種方法花費了很多變更,我不想這樣做。

所以我試圖從gridview得到一個特定的孩子,找到這個圖標並更改圖標,但返回的視圖爲空。

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

+0

請發佈getVIew方法 –

+0

@SergeyShustikov,請參閱最新的問題。 – mehdok

+0

我認爲'getChildAt(0)'返回**不是一行**對象。需要調試並看到。 –

回答

1

您可以使用getItemIdAtPosition將該ID與標頭的ID進行比較。如果它不是標題,它應該是包含你的東西的視圖。

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找頭,所以應該努力找到究竟是不是頭也。

+0

這是非常好的,完美的作品,但這裏有一個小問題,錯誤的圖標會因爲標題而更新。如果1個標題在被點擊的項目之前先前的圖標被更新。如果在項目10項目8獲得更新之前有2個標題。任何建議? – mehdok

+1

@mehdok從第一個項目開始循環,在每個不是標題的項目處增加一個計數器。當櫃檯等於你應該改變的位置時,你在正確的項目上。 – M4rtini

0

好像你正在使用錯誤的ID,你確定R.id.download_icon_button在視圖中嗎?如果不是,你必須添加它。

+0

請看到更新的問題,你可以看到id在視圖中。 – mehdok