GetView adapter is called multiple times with position 0

I am facing some problem for rendering a ListView from a dynamic layout. I don't know why it getView

is only called with position 0 and multiple times!

I have searched the internet and stackoverflow but could not find a suitable answer.

I'm actually trying to do a demo of this: http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

Notably, my main layout file is surrounded by a scroll bar.

main activity layout file:

    <ListView
        android:id="@+id/city_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/questionsList"
        android:paddingTop="20sp" >
    </ListView>

      

My layout file for list view:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="80dip" >

        <ImageView
            android:id="@+id/ImageCity"
            android:layout_width="90sp"
            android:layout_height="90sp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/ImageCity"
            android:orientation="vertical"
            android:paddingLeft="10sp">

            <TextView
                android:id="@+id/cityName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/cityLinkWiki"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:textSize="15sp" />
        </LinearLayout>

    </RelativeLayout>

      

Adapter class:

    import com.incidentreport.app.classes.objects.City;

    public class CityListAdapter extends ArrayAdapter{

        private int resource;
        private LayoutInflater inflater;
        private Context context;

        public CityListAdapter ( Context ctx, int resourceId, List objects) {

            super( ctx, resourceId, objects );
            resource = resourceId;
            inflater = LayoutInflater.from( ctx );
            context=ctx;
        }

        @Override
        public View getView ( int position, View convertView, ViewGroup parent ) {

            Log.v("adapter", "pos: " + position + "#" + resource);
            /* create a new view of my layout and inflate it in the row */

            convertView = ( RelativeLayout ) inflater.inflate( resource, null );

            /* Extract the city object to show */
            City city = (City)getItem( position );

            /* Take the TextView from layout and set the city name */
            TextView txtName = (TextView) convertView.findViewById(R.id.cityName);

            txtName.setText(city.getName());

            /* Take the TextView from layout and set the city wiki link */
            TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
            txtWiki.setText(city.getUrlWiki());

            /* Take the ImageView from layout and set the city image */
            ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);

            return convertView;
        }
    }

      

main snipps opcode:

    List listCity= new ArrayList();

    listCity.add(new City("London","http://en.wikipedia.org/wiki/London","london"));
    listCity.add(new City("Rome","http://en.wikipedia.org/wiki/Rome","rome"));
    listCity.add(new City("Paris","http://en.wikipedia.org/wiki/Paris","paris"));

    ListView listViewCity = ( ListView ) findViewById( R.id.city_list);

    listViewCity.setAdapter( new CityListAdapter(this, R.layout.layout_city, listCity     ) );

      

+3


source to share


5 answers


Ok I figured out the problem by extending the ListView as much as possible. The point is to say by giving dynamic full height so that the whole subject becomes visible.

I followed the following solution:



Calculate the size of a list view or how to fully expand it

+2


source


Try this way, hope it helps you solve your problem.



public class CityListAdapter extends BaseAdapter{
    private Context context;
    private List objects;

    public CityListAdapter ( Context context, int resourceId, List objects) {
        this.context=context;
        this.objects=objects;

    }

    @Override
    public int getCount() {
        return objects.size();
    }

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

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public View getView ( int position, View convertView, ViewGroup parent ) {
        ViewHolder holder;

        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_city,null);
            holder.txtName = (TextView) convertView.findViewById(R.id.cityName);
            holder.txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
            holder.imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(((City)getItem(position)).getName());
        holder.txtWiki.setText(((City)getItem(position)).getUrlWiki());

        return convertView;
    }

    class ViewHolder{
        TextView txtName;
        TextView txtWiki;
        ImageView imageCity;
    }
}

      

+1


source


Use a template ViewHolder

to improve productivity.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

static class ViewHolder
{
     TextView txtName,txtWiki;
     ImageView imageCity; 
}

      

Change getView to

    @Override
    public View getView ( int position, View convertView, ViewGroup parent ) {

        ViewHolder holder;
        if(convertView == null)
        {

        convertView = ( RelativeLayout ) inflater.inflate( resource, parent, false );
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.cityName);  
        holder.txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);  
        holder.imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
        convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag(); 
        } 

        City city = (City)getItem( position );

        holder.txtName.setText(city.getName());
        holder.txtWiki.setText(city.getUrlWiki());

        return convertView;
    }

      

ListView recyclues view. You can also read

How ListView recycling works

+1


source


I faced this problem. My list mostly works, but there is a specific sequence of actions that will make certain items disappear. Clicking on them subsequently throws a NullPointerException.

Here are the steps to reproduce the error:

  • Drag the item up.
  • Drag another item up or down.
  • The item at the top will disappear.
  • Drag another item up or down.
  • The top item appears.
  • Behavior continues if you go to step 2

After debugging, I found that my StableArrayAdapter.getView () method is being called twice, only for an empty item at the top of the list.

To fix this, in masum7's answer, I set the layout_height for my DynamicListView to "wrap_content".

+1


source


Try to get the layout inflated like LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); This might work

0


source







All Articles