适配器 getView 被多次调用,位置为 0

mas*_*um7 5 java android

我在从动态布局渲染 ListView 时遇到了一些问题。我不知道为什么getView只用位置 0 调用了几次!

我在互联网和 stackoverflow 上搜索过,但找不到合适的答案。

我实际上正在尝试做一个演示:http : //www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

值得注意的是,我的主布局文件被滚动条包围。

主要活动布局文件:

    <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>
Run Code Online (Sandbox Code Playgroud)

我的列表视图布局文件:

    <?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>
Run Code Online (Sandbox Code Playgroud)

适配器类:

    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's object to show */
            City city = (City)getItem( position );

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

            txtName.setText(city.getName());

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

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

            return convertView;
        }
    }
Run Code Online (Sandbox Code Playgroud)

主要活动代码片段:

    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     ) );
Run Code Online (Sandbox Code Playgroud)

mas*_*um7 3

好吧,我通过尽可能扩展 ListView 解决了这个问题。意思是说,给出动态的完整高度,以便所有项目都可见。

我遵循以下解决方案:

计算列表视图的大小或如何让它完全展开