2017-10-13 172 views
0

我想在自定義適配器中檢索textView,但由於某種原因返回null。你們能解釋爲什麼嗎?我是Android的新手,並且卡在這裏。我還附加了適配器正在連接的視圖,其名爲profile_listitem.xml幷包含我想要的textView。findViewbyId在自定義適配器中返回null

謝謝

AdapterProfile.xml

public AdapterProfiles(Context context,ArrayList<Profile> profilesArray) 
{ 
    super(context, R.layout.profile_listitem, profilesArray); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    Profile p= getItem(position); 

    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.profile_listitem , parent, false); 
    } 
    TextView textAppName=(TextView)convertView.findViewById(R.id.textViewApp); 
    ToggleButton appStatus =(ToggleButton)convertView.findViewById(R.id.toggleAppStatus); 

    if(convertView.findViewById(R.id.textViewApp)==null) 
     Log.d("Profile","Profile is null"); 

    textAppName.setText(p.getName()); 
    appStatus.setChecked(p.isActive()); 

    return convertView; 
} 

profile_listitem.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@color/background" 
    android:textColor="@color/blue" 
    android:textSize="36dp" 
    android:id="@+id/textViewAppName"/> 
<ToggleButton 
    android:layout_width="72dp" 
    android:layout_height="48dp" 
    android:layout_alignParentEnd="true" 
    android:textColor="@color/white" 
    android:background="@color/blue" 
    android:id="@+id/toggleAppStatus"/> 

回答

0

因爲你已經使用

R.id.textViewApp

,而不是你在xml文件即

R.id.textViewAppName

的TextView textAppName =(TextView的)convertView.findViewById定義了什麼( 'R.id.textViewApp');

相關問題