2017-10-13 11 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');

関連する問題