Android:在自定义适配器中调用 getView() 两次

2024-01-02

我正在将自定义 SimpleCursorAdapter 设置为 ListView。由于某种原因,FriendAdapter 的 getView() 会针对数据库中的每个项目调用两次。经过一番调查(我的 contact_list.xml 中没有wrapp_content),我仍然不明白为什么。

可能是什么原因?有人可以帮忙吗?

Thanks

ContactSelection.java

public class ContactSelection extends ListActivity {

    private WhipemDBAdapter mDbHelper;  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDbHelper = new WhipemDBAdapter(this);
        mDbHelper.open();     

        setContentView(R.layout.contact_list);        

        Cursor c = mDbHelper.fetchAllFriends();
        startManagingCursor(c);     
        String[] from = new String[] {};
        int[] to = new int[] {};

        setListAdapter(new FriendAdapter(this, R.layout.contact_row, c, from, to));

        getListView().setItemsCanFocus(false);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDbHelper.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDbHelper.close();
    }
}

FriendAdapter.java

public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {

    private Context mContext;
    private int mLayout;
    private Cursor mCursor;
    private int mNameIndex;
    private int mIdIndex;
    private LayoutInflater mLayoutInflater; 
    private final ImageDownloader imageDownloader = new ImageDownloader();  

    private final class ViewHolder {
        public TextView name;
        public ImageView image;
        public CheckBox checkBox;
    }

    public FriendAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        this.mContext = context;
        this.mLayout = layout;
        this.mCursor = c;
        this.mNameIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_NAME);
        this.mIdIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_FB_ID);
        this.mLayoutInflater = LayoutInflater.from(context);        
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = mLayoutInflater.inflate(mLayout, null);

                viewHolder = new ViewHolder();
                viewHolder.name = (TextView) convertView.findViewById(R.id.contact_name);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.contact_pic);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
                viewHolder.checkBox.setOnClickListener(this);

                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            String name = mCursor.getString(mNameIndex);
            String fb_id = mCursor.getString(mIdIndex);         
            boolean isChecked = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);

            viewHolder.name.setText(name);
            imageDownloader.download("http://graph.facebook.com/"+fb_id+"/picture", viewHolder.image);

            viewHolder.checkBox.setTag(fb_id);
            viewHolder.checkBox.setChecked(isChecked);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        CheckBox cBox = (CheckBox) v;
        String fb_id = (String) cBox.getTag();

        if (cBox.isChecked()) {
            if (!((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
                ((GlobalVars) mContext.getApplicationContext()).addSelectedFriend(fb_id);
        } else {
            if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
                ((GlobalVars) mContext.getApplicationContext()).removeSelectedFriend(fb_id);
        }

    }
}

contact_row.xml

<?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="horizontal">

    <ImageView
        android:id="@+id/contact_pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/contact_name"        
        android:textSize="10sp"
        android:singleLine="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

联系人列表.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView
        android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No items"/>
</LinearLayout>

这是正常现象,当您有一个列表视图时可能会发生height=wrap_content(除其他外):

看最后一个帖子:http://groups.google.com/group/android-developers/browse_thread/thread/4c4aedde22fe4594 http://groups.google.com/group/android-developers/browse_thread/thread/4c4aedde22fe4594

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:在自定义适配器中调用 getView() 两次 的相关文章

随机推荐

  • 当我使用重定向网址时,为什么 %20 会变成 %2520?

    这按预期工作 它返回一条记录 https terraref ncsa illinois edu bety api v1 search json sitename Season 204 limit 1 这不 当我使用基本网址 terraref
  • 通过 XPath 在任意深度选择 XML 节点

    拥有一个 XML 文档 我想要获取具有特定名称的第一个节点 无论它包含在哪个嵌套深度中 我尝试了几件事但没有成功 var node1 doc SelectSingleNode Shortcut var node2 doc SelectSin
  • 如何将 PickedFile 类型的设置图像保存到 Flutter 中的图像?

    我想设置从相机获取图像并将其设置为以下代码 image DecorationImage image FileImage file 所以我这样做了 File file await ImagePicker pickImage source Im
  • 如何在 PHP 中实现推送通知

    我使用 ajax 已经有一段时间了 我用它做了很多漂亮的工作 但是我最近的挑战是推送通知 我想实现一个站点 不需要每个时间段都调用服务器 而是仅当特定数据库字段有更新时才调用服务器 并且我想在PHP javascript 和 或 jquer
  • 在 ColdFusion 中以编程方式验证邮件服务器连接

    我正在使用自定义 SMTP 服务器 并且希望在用户输入自己的服务器凭据时验证连接 与 Adob e CF 和 Railo 在添加邮件服务器时允许执行的检查类型完全相同 当然 这并不能保证delivery将会工作 但至少要检查输入的服务器 用
  • socket.io 是否重新连接重新运行连接?

    我构建了一个简单的 Web 应用程序 它使用 socket io 通过 Node js 服务器进行一些通信 当用户连接时 节点会传回信息 告诉客户端订阅某些事件 效果很好 但是 如果您让浏览器处于空闲状态 客户端最终会订阅事件两次 订阅过程
  • int 到 double 的无效转换异常

    也许我疯了 但我认为这是一个有效的演员阵容 new int 1 2 3 4 5 Cast
  • Silverstripe 的 Coda 语法高亮显示

    我刚刚搬到一台新机器上 在我的旧机器上 我有 Silverstripe 模板 ss 文件 遵循 Coda 中的 HTML 语法突出显示 我一辈子都不记得我是如何打开它的 不过我记得这是我自己做的 谷歌没有找到任何结果 有什么建议么 更新 真
  • “在 SPARK Ada 中接受挑战”- 后置条件下的总和鬼函数有意外行为

    我正在 SPARK Ada 中编写一个软件 它需要后置条件来验证函数返回值是否等于数组的求和值 在证明函数所在的文件后 我不断收到一个错误 该错误并没有完全加起来 没有双关语的意图 我将发布代码的屏幕截图以便更好地查看 大小为 10 的数组
  • 如何使用 Perl 搜索文件中两个时间戳之间的行?

    在 Perl 中 我尝试读取日志文件 并且仅打印在两个特定时间之间具有时间戳的行 时间格式为 hh mm ss 并且这始终是每个日志上的第三个值 例如 我将搜索介于 12 52 33 到 12 59 33 之间的行 我是 Perl 新手 不
  • 将多个字符串传递给 string::find 函数

    是否可以以某种方式将多个字符串传递给 string find 函数 例如 要查找字符串 我可以使用以下命令 str find a string 我想做的是这样的 str find a string another string yet an
  • 验证元素在量角器中消失

    出于等待目的 我使用这种等待函数 browser wait function return browser isElementPresent by repeater recentName in recentNames row 0 10000
  • 为什么 Kotlin 的 null 安全性不能与局部变量初始化器一起正常工作?

    请看这段代码 fun localVarNullSafety1 var number Double 3 0 val sum 2 0 number does not compile Type mismatch inferred type is
  • 发送JSON数据Servlet Applet通信示例

    您好 我正在开发一个应用程序 其中 servlet 必须将数据 来自数据库 发送到小程序 我已经休养了这个link https stackoverflow com questions 6769255 applet servlet commu
  • 数据表分页不起作用?

    我的 html 页面包含一个表格 我使用 dataTable 插件进行分页 1 https i stack imgur com O2C1e png 1 https i stack imgur com O2C1e pnghttps datat
  • 在 cython 中使用 typedef'd 结构

    我在头文件 dcm h 中有以下定义 typedef struct double alpha double gamma double tau ThetaDCM 我想将它导入到 cython 中 所以我有 cdef extern from d
  • 如何将 Object.values 与打字稿一起使用?

    我正在尝试从对象中形成逗号分隔的字符串 const data Ticket 1 pdf 8e6e8255 a6e9 4626 9606 4cd255055f71 pdf Ticket 2 pdf 106c3613 d976 4331 ab0
  • 如何生成WM_SEC.AUTH_SIGNATURE?

    有谁有可以为 Walmart API 生成此标头的 python 代码吗 WM SEC AUTH SIGNATURE 我试图理解 java 示例 但我没有任何运气 因为我没有 Java 经验 如果有人知道需要签名的字符串的格式 我可能可以从
  • iPad ios 8.4 中不会触发窗口 onload 事件

    我面临以下奇怪的问题 功能 当我打开我的网站页面时 该页面包含许多图像并使用 Javascript jQuery 作为客户端功能 单击每个图像时 所有其他图像都会更改其不透明度 并且所选图像会显示 div 包含一些信息和图像的视频 我使用了
  • Android:在自定义适配器中调用 getView() 两次

    我正在将自定义 SimpleCursorAdapter 设置为 ListView 由于某种原因 FriendAdapter 的 getView 会针对数据库中的每个项目调用两次 经过一番调查 我的 contact list xml 中没有w