java - ListView - getView 被调用太多次

标签 java android android-listview android-arrayadapter

我知道关于“getView 被调用了几次”这个问题几乎没有问题,但我的问题有点不同。

我有一个带有自定义行的自定义 listView(使用了 row_layout.xml)。 它通常运作良好。一开始我遇到了多次调用 getView 的问题,后来使用我在 stackoverflow 中看到的其中一种方法解决了这个问题。 (使用“usedPositions”数组)。

现在,我在日志中看到这种情况:getView pos 0、getView pos 1、getView pos 0、getView pos 1。 这导致我的行数增加了一倍。只有当我调用一个涵盖当前 Activity 的新 Activity 然后关闭该 Activity 时,它才会发生。 (例如,打开相机 Activity ,然后将其关闭)。

我将展示我的代码:

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    Toast toast = Toast.makeText(this.context, "getView " + position, 1000);
    toast.show();
    String pos = Integer.toString(position);
    if (!usedPositions.contains(pos)) { 

        CardHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new CardHolder();
            //holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtCouponTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtBusinessName = (TextView)row.findViewById(R.id.txtBusinessName);
            row.setTag(holder);
        }
        else
        {
            holder = (CardHolder)row.getTag();
        }

        Card card = data.get(position);
        holder.txtCouponTitle.setText(card.couponTitle);
        holder.txtBusinessName.setText(card.businessName);
        //holder.imgIcon.setImageResource(card.icon);

        TableLayout table = (TableLayout)row.findViewById(R.id.imagesTable); 
        for (int r=1; r<=1; r++){ 
            TableRow tr = new TableRow(this.context); 
            for (int c=1; c<=10; c++){ 
                ImageView im = new ImageView (this.context); 
                im.setImageDrawable(this.context.getResources().getDrawable(c<= card.numberOfStamps ? R.drawable.stamp_red :R.drawable.stamp_grey)); 
                im.setPadding(6, 0, 0, 0); //padding in each image if needed 
                //add here on click event etc for each image... 
                //... 
                tr.addView(im, 40,40);  
            } 
            table.addView(tr); 
        } 

        // Your code to fill the imageView object content 
        usedPositions.add(pos); // holds the used position 
    } 
    else
        usedPositions.remove(pos);

    return row;
}

你能告诉我哪里出了问题吗?

最佳答案

Quoting安卓工程师RomainGuy

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.

所以你能处理的最好的是re-using the existing views (行布局)正确。

Here is another好帖子。

关于java - ListView - getView 被调用太多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157523/

相关文章:

Android: extends TabActivity deprecated,什么时候会出问题?

android - 显示 SD 卡中的所有歌曲 Genre Wise

android - MultiChoiceModeListener.onItemCheckedStateChanged() 仅在选中项目时执行

java - 如何获得 Maven 报告插件以使用 doxia API 生成报告?

java - 配置 Java MongoClient 以使用 X.509 证书进行身份验证

android - 简单的 HttpURLConnection POST 文件 multipart/form-data 从 android 到 google blobstore

android - Android 中来自 Sqlite 的 ListView

android - 淡化操作栏 Android 仅显示列表中的一项

java - 当我聚焦 jcombobox 时,我需要点击 2 次才能单击按钮

java - 将一个 JPanel 添加到另一个 JPanel 会添加填充