Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android customAdapter (BasesAdapter) getView is always returning 0

I have made a customadapter for a listview, but for some reason the getView method's parameter "position" is always 0, which makes the method pretty useless.

Here is the code of my customAdapter, extending 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;
    }
}

As you can see I am syso'ing out the position, which is always 0!

Here is the xml layput for the items in the listview:

<?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>

And here is the code where I'm calling the 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);

No matter how many items are inserted in the listview, the adapters position in the getView method is always 0. Obviously I want it to return 0 for the first item only, then 1 and 2 and so on, I can't figure out why I doesn't do that.

If there are more information needed to solve the problem, please just ask. Any help will be greatly appreciated.

EDIT:

I just discovered something really odd. I tried adding another item to the list like this:

    TrainingPass trainingPass3 = new TrainingPass();
    trainingPass3.trainingPassName = "ffffrwwewefefhtr";
    trainingPass3.trainingPassID = 3;

The result of this is exactly the same as before. There are only made 2 Syso's in the terminal, writing out 0 (the position variable). Also the listview only has 2 items in it, even though I've added 3 to the adapter.

This must mean that the getview is called twice for one item and then the rest of the items are not called.

But still, I don't know why this is happening.

like image 431
Langkiller Avatar asked Dec 14 '22 15:12

Langkiller


2 Answers

This error will happen if you put a ListView inside a ScrollView. ListView itself does vertical scroll so it must not be put into a ScrollView.

An adapter will call getView when listView need new item to show. So, if your listView not scroll, no new item be created, and no call to getView.

like image 178
delkant Avatar answered Dec 17 '22 05:12

delkant


replace this

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

with this

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

Edit2: comparing ith my baseadapter i suggest the following

replace this

holder.trainingPass = this.getItem(position);

by this

holder.trainingPass = items.getItem(position);
like image 28
Charaf Eddine Mechalikh Avatar answered Dec 17 '22 06:12

Charaf Eddine Mechalikh