我创建了一个自定义适配器,并为其提供示例数据以检查它是否正常工作...适配器有 if else 语句
if () {
else if (){
}
即使第一个 if 语句为 true,它也会继续在第二个条件下运行代码。 “它只显示 Activity ID
示例数据
public class ActivityListArray {
public static ArrayList<Activity> getActivitiesList() {
/* Activity (buildingnumber, acitivytname, activityid) */
final ArrayList<Activity> soleyaactivities = new ArrayList<Activity>();
activities.add(new Activity("E1", "(BS-C-E1) Backfilling Up To SOG", "A9360"));
activities.add(new Activity("D1", "(BS-C-D1) Insulation for P.C SOG", "A10320"));
return activities;
}
}
适配器代码
public class ActivityAdapter extends ArrayAdapter<Activity> {
public Activity currentActivity;
public String BuildingNumber;
public String currentbuildingnumber;
public ActivityAdapter(Context context, ArrayList<Activity> Activities, String adapterbuildingnumber) {
super(context, 0, Activities);
// This is the Building number of the Clicked Building
currentbuildingnumber = adapterbuildingnumber;
// This is the Building number of the Activity
BuildingNumber = currentActivity.getBuildingNumber();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (BuildingNumber.equals(currentbuildingnumber)) {
// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list, parent, false);
}
// Get the {@link Word} object located at this position in the list
currentActivity = getItem(position);
TextView Act_ID = (TextView) v.findViewById(R.id.emptyslot);
TextView Act_Name = (TextView) v.findViewById(R.id.Act_Name);
Act_ID.setText(String.valueOf(currentActivity.getActivityID()));
Act_Name.setText(String.valueOf(currentActivity.getActivityName()));
}
}
else if (BuildingNumber != currentbuildingnumber){
// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list_empty, parent, false);
}
// Get the {@link Word} object located at this position in the list
currentActivity = getItem(position);
TextView Act_ID = (TextView) v.findViewById(R.id.emptyslot);
Act_ID.setText(String.valueOf(currentActivity.getActivityID()));
}
return v;
}
}
project_actvitiy_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/backgroundColor"
android:orientation="vertical"
tools:context=".SoleyaProjectSchedule">
<TextView
android:id="@+id/Act_ID"
style="@style/ProjectUpdate_Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="TextView" />
<TextView
android:id="@+id/Name"
style="@style/ProjectUpdate_Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:textColor="@color/blue"
android:textStyle="bold"
android:text="Activity Name" />
</LinearLayout>
project_actvitiy_list_empty.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/backgroundColor"
android:orientation="vertical"
tools:context=".SoleyaProjectSchedule">
<TextView
android:id="@+id/emptyslot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
最佳答案
您应该检查此语句是否返回 true:
if (BuildingNumber.equals(currentbuildingnumber))
也许它返回 false。
关于java - 在 android studio 中运行时适配器使用错误的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51734954/