2015-08-03 43 views
0

嗨,大家好我已經爲我的小應用程序創建了自定義行和自定義適配器。當我嘗試在自定義適配器中使用findViewById方法來獲取對我的自定義佈局中的TextView的引用時,問題就在眼前。 Android工作室把這封信變成紅色,並給我一個錯誤,說無法解析findViewById(int)方法。 這裏是我的自定義適配器代碼:findViewById()無法在我的自定義適配器中解析

package com.example.opeyemi.storytime; 

import android.app.Activity; 
import android.content.Context; 
import android.text.Layout; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


import java.util.ArrayList; 
public class listViewBookAdapter extends ArrayAdapter<Book> { 


    public listViewBookAdapter(Context context, ArrayList<Book>books) { 
     super(context,R.layout.row_layout,books); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflator=LayoutInflater.from(getContext()); 
     View bookRow=inflator.inflate(R.layout.row_layout, parent, false); 
     Book book=getItem(position); 
     TextView bookNameLabel=(TextView)findViewById(R.id.bookNameLabel); 
     return bookRow; 

    } 
} 
+0

'ArrayAdapter'沒有方法'findViewById' ...但是'View'有它......問bazillion次 – Selvin

+1

'TextView bookNameLabel =(TextView)bookRow.findViewById(R.id.bookNameLabel);'。另外檢查查看持有人模式 – Raghunandan

回答

1

只需添加bookRowfindViewById

View bookRow=inflator.inflate(R.layout.row_layout, parent, false); 
     Book book=getItem(position); 
     TextView bookNameLabel=(TextView)bookRow.findViewById(R.id.bookNameLabel); 
+1

謝謝你們的伎倆 – opeyemi

+0

@opeyemi很高興收到您的評論 –

0
bookRow.findViewById(R.id.bookNameLabel); 

,因爲你是充氣佈局返回佈局來看,它是分配給bookRow作爲參考變量。