【问题标题】:Adapter getView is called multiple times with position 0适配器 getView 被多次调用,位置为 0
【发布时间】:2014-11-04 23:27:07
【问题描述】:

我在从动态布局呈现 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>

我的列表视图布局文件:

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

适配器类:

    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;
        }
    }

主要活动代码片段:

    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     ) );

【问题讨论】:

  • 使用 ViewHolder 模式。使用inflater.inflate( resource, parent,false);
  • Notably, my main layout file is surrounded by scrollbar.。 ?你的意思是?然后展示它。
  • @Raghunandan 我试过这个.. 现在 getView 只被调用一次,但它应该在不同的位置被调用 3 次

标签: java android


【解决方案1】:

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

我遵循以下解决方案:

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

【讨论】:

  • 我遇到了同样的问题。就我而言,我将 ListView 和一些额外的组件放在 ScrollView 中并设置 ScrollView 包装内容
【解决方案2】:

使用ViewHolder 模式以获得更好的性能。

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

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

将 getView 更改为

    @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 回收视图。您可能还想阅读

How ListView's recycling mechanism works

【讨论】:

  • 嗨,伙计,我试过了..但仍然面临同样的问题。我在 getView 中放了一个 Log.v。似乎 getView 被调用为 0 09-11 02:40:13.006 2508-2508/V/test:#0 09-11 02:40:13.063 2508-2508/V/test:#0 09-11 02:40 :13.066 2508-2508/ V/test: #0 09-11 02:40:13.068 2508-2508/ V/test: #0 09-11 02:40:13.070 2508-2508/ V/test: #0跨度>
  • @masum7 它应该可以工作。永远不要将列表视图放在滚动视图中。
  • 我有什么替代品?而不是 ListView?我必须有一个滚动视图
  • @masum7 您可以将视图作为页眉和页脚添加到列表视图。 ListView 将自行滚动。所以永远不要在 scorllview 里面放一个列表视图
  • 其实我的主要活动布局有很多输入,需要滚动视图
【解决方案3】:

试试这个方法,希望能帮助你解决问题。

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;
    }
}

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。我的列表视图大部分都在工作,但是有一定的动作序列会使某些项目消失。之后单击它们会导致 NullPointerException。

    以下是重现该错误的步骤:

    1. 将项目拖到顶部。
    2. 向上或向下拖动另一个项目。
    3. 顶部的项目将消失。
    4. 向上或向下拖动另一个项目。
    5. 顶部的项目将重新出现。
    6. 如果您转到第 2 步,行为将继续

    调试后发现我的 StableArrayAdapter.getView() 方法被调用了两次,只针对列表顶部的空白项。

    为了解决这个问题,根据 masum7 的回答,我将 DynamicListView 的 layout_height 设置为“wrap_content”。

    【讨论】:

      【解决方案5】:

      尝试将布局充气器作为 LayoutInflater inflater = (LayoutInflater)context.getSystemService (上下文.LAYOUT_INFLATER_SERVICE); 这可能有效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        相关资源
        最近更新 更多