我已经为ListView制作了一个自定义适配器,但由于某种原因,getView方法的参数“position”始终为0,这使得该方法几乎无用。
这是我的自定义适配器的代码,扩展了BaseAdapter:
public class CreateProgramAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items) {
this.context = context;
this.items = items;
this.inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public TrainingPass getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TrainingPassHolder holder;
if(convertView == null) {
view = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, parent, false);
holder = new TrainingPassHolder();
System.out.println(position);
holder.trainingPass = this.getItem(position);
holder.trainingPassContainer = (RelativeLayout) view.findViewById(R.id.trainingPassContainer);
holder.trainingPassHeadlineShowHide = (TextView) view.findViewById(R.id.trainingpass_headline);
holder.noExercisesYet = (TextView) view.findViewById(R.id.txt_no_exercises_yet);
holder.exerciseSearchField = (EditText) view.findViewById(R.id.trainingpass_exercise_searchfield);
holder.trainingPassSetsListView = (ListView) view.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) view.findViewById(R.id.exercises_listview);
holder.addSetButton = (Button) view.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) view.findViewById(R.id.add_exercise_button);
holder.trainingPassContainer.setTag(holder.trainingPass);
holder.trainingPassHeadlineShowHide.setTag(holder.trainingPass);
holder.noExercisesYet.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.trainingPassSetsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
view.setTag(holder);
} else {
view = convertView;
holder = (TrainingPassHolder)view.getTag();
}
setupItems(holder);
return view;
}
private void setupItems(TrainingPassHolder trainingPassHolder) {
int id = trainingPassHolder.trainingPass.trainingPassID;
TextView showHide = trainingPassHolder.trainingPassHeadlineShowHide;
showHide.setText("Trainingpass " + id);
}
private class TrainingPassHolder {
TrainingPass trainingPass;
RelativeLayout trainingPassContainer;
TextView trainingPassHeadlineShowHide;
TextView noExercisesYet;
EditText exerciseSearchField;
ListView trainingPassSetsListView;
ListView exercisesListView;
Button addSetButton;
Button addExerciseButton;
}
}
您可以看到我正在输出位置,它始终为0!
以下是列表视图中项目的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="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="18dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:height="35dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textIsSelectable="true"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:background="@android:color/holo_blue_dark"
android:height="30dp"
android:minWidth="50dp"
android:layout_alignParentTop="true"
android:layout_above="@+id/trainingPassContainer"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/trainingpass_headline"
android:layout_alignLeft="@+id/trainingpass_headline"
android:layout_alignStart="@+id/trainingpass_headline"
android:id="@+id/trainingPassContainer"
android:background="@color/blue_bg_neutral"
android:layout_marginBottom="10dp"
android:visibility="visible">
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name of exercise"
android:padding="6dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@color/blue_bg_inputField"
android:layout_margin="10dp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/sets_container"
android:background="@color/blue_bg_inputField"
android:layout_margin="10dp"
android:padding="10dp">
<TextView
android:id="@+id/txt_sets_for_exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sets for exercise"
android:textSize="14dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingBottom="10dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_sets_listview"
android:nestedScrollingEnabled="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/txt_sets_for_exercise"
android:layout_below="@+id/txt_sets_for_exercise" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add set"
android:id="@+id/add_set_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add exercise"
android:id="@+id/add_exercise_button"
android:layout_below="@+id/add_set_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/sets_container"
android:id="@+id/exercises_container"
android:background="@color/blue_bg_inputField"
android:layout_margin="10dp"
android:padding="10dp">
<TextView
android:id="@+id/txt_exercises"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercises"
android:textSize="14dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingBottom="10dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/txt_no_exercises_yet"
android:layout_alignEnd="@+id/txt_no_exercises_yet" />
<TextView
android:id="@+id/txt_no_exercises_yet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No exercises yet"
android:textSize="14dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/txt_exercises"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exercises_listview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/txt_no_exercises_yet"
android:layout_above="@+id/txt_no_exercises_yet" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
这里是我调用CreateProgramAdapter的代码:
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.trainingPassName = "wefwef";
trainingPass.trainingPassID = 1;
TrainingPass trainingPass2 = new TrainingPass();
trainingPass2.trainingPassName = "rwefhtr";
trainingPass2.trainingPassID = 2;
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
无论在listview中插入多少项,适配器在getView方法中的位置始终为0。显然,我希望它仅返回第一项为0,然后是1、2等等,但我无法弄清楚为什么它不这样做。
如果需要更多信息来解决问题,请随时提问。任何帮助都将不胜感激。
编辑:
我刚刚发现了一些非常奇怪的事情。我尝试像这样添加另一个项目到列表中:
TrainingPass trainingPass3 = new TrainingPass();
trainingPass3.trainingPassName = "ffffrwwewefefhtr";
trainingPass3.trainingPassID = 3;
这样的结果与之前完全相同。终端只输出了两个Syso,写出了0(位置变量)。即使我已经向适配器中添加了3个项目,列表视图中也只有2个项目。
这意味着getview为一个项目调用了两次,然后其余项目没有被调用。
但是,我仍然不知道为什么会发生这种情况。