Android自定义适配器(BaseAdapter)getView始终返回0

时间:2015-04-21 21:46:47

标签: android android-listview baseadapter custom-adapter getview

我为listview创建了一个customadapter,但由于某种原因,getView方法的参数“position”始终为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 layput:

<?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两次,然后不调用其余项目。

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

5 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:3)

我找到了解决方案。虽然它对我没有意义,但它是:

通过将RelativeLayout容器的高度和宽度从wrap_content / match_parent更改为fill_parent,问题得以解决:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal"
    android:padding="10dp">

感谢所有发布答案和评论的人,以帮助解决问题。我很感激。

答案 2 :(得分:2)

替换此

@Override
public long getItemId(int position) {
    return 0;
}

用这个

@Override
public long getItemId(int position) {
    return position;
}

编辑2:与我的baseadapter比较我建议如下

替换此

holder.trainingPass = this.getItem(position);

由此

holder.trainingPass = items.getItem(position);

答案 3 :(得分:1)

在您对 CreateProgramAdapter 的调用中,我怀疑您没有为适配器传递正确的上下文。 改变自:

new CreateProgramAdapter(CreateProgramActivity.this, ...

TO:

new CreateProgramAdapter(getActivity(), ...

注意:

  • CreateProgramActivity在范围内必须是静态的,但我觉得它不是一直都是正确的活动,有时它更具动态性。如果代码位于片段中,方法 getActivity 更准确,更容易。但是,获得正确的活动可能很困难。让我们试一试,看看......

答案 4 :(得分:0)

将ListView小部件的布局宽度和高度更改为“match_parent”。