gpt4 book ai didi

Android customAdapter (BasesAdapter) getView 总是返回 0

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:42 24 4
gpt4 key购买 nike

我为 ListView 制作了一个自定义适配器,但由于某种原因,getView 方法的参数“位置”始终为 0,这使得该方法非常无用。

这是我的 customAdapter 的代码,扩展了 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!

这是 ListView 中项目的 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;

这个结果和之前完全一样。终端里只做了2个Syso,写出0(位置变量)。此外, ListView 中只有 2 个项目,即使我已经向适配器添加了 3 个项目。

这一定意味着为一个项目调用了两次 getview,然后没有调用其余的项目。

但是,我仍然不知道为什么会这样。

最佳答案

如果将 ListView 放在 ScrollView 中,就会发生此错误。 ListView 本身会垂直滚动,因此不能将其放入 ScrollView。

当 listView 需要显示新项目时,适配器将调用 getView。因此,如果您的 listView 不滚动,则不会创建新项目,也不会调用 getView。

关于Android customAdapter (BasesAdapter) getView 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com