自定义适配器中的 findViewbyId returns null
findViewbyId in custom adapter returns null
我正在尝试在自定义适配器中检索 textView,但由于某种原因它返回 null。你们能解释一下为什么吗?我是 Android 的新手,被困在这里。我还附加了适配器连接到的视图,它称为 profile_listitem.xml 并包含我想要的 textView。
谢谢
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"/>
因为你用过
R.id.textViewApp
而不是您在 xml 文件中定义的内容,即
R.id.textViewAppName
TextView textAppName=(TextView)convertView.findViewById('R.id.textViewApp');
我正在尝试在自定义适配器中检索 textView,但由于某种原因它返回 null。你们能解释一下为什么吗?我是 Android 的新手,被困在这里。我还附加了适配器连接到的视图,它称为 profile_listitem.xml 并包含我想要的 textView。
谢谢
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"/>
因为你用过
R.id.textViewApp
而不是您在 xml 文件中定义的内容,即
R.id.textViewAppName
TextView textAppName=(TextView)convertView.findViewById('R.id.textViewApp');