Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getView() method of ArrayAdapter repeats first two or three items in list

I extended an ArrayAdapter with an overridden getView() method to fill my ListView object, but the adapter will grab the first two or three objects from the ArrayList I pass to it, then just repeat those in seemingly random order for the entire length of the list. Furthermore, when I scroll up and down in the ListView, some rows change from one list item to another. The ArrayList<Credit> object gets populated from a JSONObject, and to the best of my knowledge is correct prior to being passed into the ArrayAdapter.

My custom ArrayAdapter

private class CreditArrayAdapter extends ArrayAdapter<Credit> {
    private ArrayList<Credit> credits;
    private int creditCount;

    public CreditArrayAdapter(Context ctx, int textViewResourceId, List<Credit> items) {
        super(ctx, textViewResourceId, items);
        credits = (ArrayList<Credit>) items;
        creditCount = credits.size();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_movie_medium, null);
            Credit current = credits.get(position);
            Log.d(tag, "current credit: " + current.toString());
            ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
            try {
                creditPicture.setImageBitmap(current.getPosterSmall());
            } catch(Exception e) {
                Log.d(tag, "failed to set cast member picture");
                e.printStackTrace();
            }
            TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
            castMemberName.setText(current.toString());
        }
        return v;
    }
    @Override
    public int getCount() {
        return creditCount;
    }
}

being called by:

appearsIn = (ListView) findViewById(R.id.listview_appears_in);
List<Credit> credits = searcher.getCreditsByPersonId(personId);
CreditArrayAdapter creditAdapter = new CreditArrayAdapter(getBaseContext(), R.layout.list_item_movie_medium, credits);
creditAdapter.notifyDataSetChanged();
appearsIn.setAdapter(creditAdapter);

The layout containing the ListView:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:layout_centerHorizontal="true"
              android:weightSum="2">
    <ScrollView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">
            <ImageView android:id="@+id/image_person_info"
                       android:layout_height="wrap_content"
                       android:layout_width="wrap_content"
                       android:contentDescription="person image"/>
            <TextView android:id="@+id/text_person_name"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="30dp"/>
            <TextView android:id="@+id/text_person_birth"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="20dp"/>
            <TextView android:id="@+id/text_person_death"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="20dp"/>
            <TextView android:id="@+id/text_person_bio"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="16dp"/>
        </LinearLayout>
    </ScrollView>

    <ListView android:id="@+id/listview_appears_in"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"/>
</LinearLayout>

My list item layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">
    <ImageView android:id="@+id/image_poster_thumbnail"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"/>
    <TextView android:id="@+id/text_credit_info"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"/>
</LinearLayout>
like image 797
Max Avatar asked Dec 27 '22 21:12

Max


1 Answers

change the getView like this:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item_movie_medium, null);
    }
    Credit current = credits.get(position);
    Log.d(tag, "current credit: " + current.toString());
    ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail);
    try {
       creditPicture.setImageBitmap(current.getPosterSmall());
    } catch(Exception e) {
        Log.d(tag, "failed to set cast member picture");
        e.printStackTrace();
    }
    TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info);
    castMemberName.setText(current.toString());

    return v;
}

Your problem was you were not doing all the code you needed to set the View object correctly when convertView was != null.

like image 106
FoamyGuy Avatar answered Apr 27 '23 01:04

FoamyGuy