草庐IT

android - 自定义 ArrayAdapter getView 未被调用——为什么不呢?

coder 2023-12-27 原文

我在另一个 Activity 类中使​​用了相同的模式,并且效果很好。但是在这个类(也是一个 Activity)中,getView 从不被调用。我通过记录 fbFriendMatchAdapter.getCount() 确认适配器中有 9 个项目。我试过将数据源更改为 String[] 变量,但这对(缺少)getView 没有影响。

如果有任何建议,我将不胜感激!我已经彻底研究过了。这个问题有很多种,但没有一个能解决我的问题。这就是为什么我要发布一个新问题。

//Here is the ArrayList data source definition.
//It is loaded using a for loop with 9 values.
private List<String> fbFriendMatchAdapter=new ArrayList<String>();


// Here is the Custom Array Adapter
ArrayAdapter<String> fbFriendMatchAdapter = new ArrayAdapter<String>(this,
        R.layout.row_left, R.id.ListViewName, fbFriendPotentialMatchArrayList) { 

    @Override
    public View getView(final int dialogPosition, View convertView, ViewGroup listParent) {
        LayoutInflater inflater = getLayoutInflater();
        View fbFriendMatchDialogViewRow = inflater.inflate(R.layout.row_left, listParent, false); 
        Log.d(BBTAG, String.format("BBSetup getView[%s] name=%s", dialogPosition, buddyDisplayName ));

        return fbFriendMatchDialogViewRow;
    }  // [END getView]

    @Override
    public String getItem(int position) {
        return fbFriendPotentialMatchArrayList.get(position);
    }

};

//Here is the dialog that includes the ArrayAdapter:
AlertDialog fbFriendsMatchDialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.PetesSpinnerReplacement))
        .setTitle("Select Correct Facebook Friend")  //FYI overridden by custom title
        .setAdapter(fbFriendMatchAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int selectedFBFriend) {
                buddyFBUIDs[buddyNumber] = fbFriendUIDsList.get(selectedFBFriend);  
            }
        })
        .setMessage("No matches found")
        .setCancelable(true)
        .setPositiveButton("Set as Facebook Friend", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int iFriend) {
                 dialog.dismiss();
             } 
        })
        .setNegativeButton("Friend Not Listed", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                buddyFBUIDs[buddyNumber] = "n/a";  //record lack of FB uid
            } 
        })  
        .create();  //build dialog
fbFriendsMatchDialog.show();  // now show dialog 

最佳答案

我建议不要为此尝试内联 ArrayAdapter 子类,因为我相信 ArrayAdapter 对您的布局和数据的结构做出了一些假设,这可能会让您感到困惑离开。编写您自己的自定义适配器非常简单,我几乎建议从不使用ArrayAdapter(除了最简单的用例)。只是子类 BaseAdapter 和四个必需的方法,然后使用它。像这样:

private class MatchAdapter extends BaseAdapter {
    private List<String> mItems;
    private LayoutInflater mInflater;

    public MatchAdapter (Context c, List<String> items) {
        mItems = items;

        //Cache a reference to avoid looking it up on every getView() call
        mInflater = LayoutInflater.from(c); 
    }

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

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

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

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        //If there's no recycled view, inflate one and tag each of the views
        //you'll want to modify later
        if (convertView == null) {
            convertView = mInflater.inflate (R.layout.row_left, parent, false);

            //This assumes layout/row_left.xml includes a TextView with an id of "textview"
            convertView.setTag (R.id.textview, convertView.findViewById(R.id.textview));
        }

        //Retrieve the tagged view, get the item for that position, and
        //update the text
        TextView textView = (TextView) convertView.getTag(R.id.textview);
        String textItem = (String) getItem(position);
        textView.setText(textItem);

        return convertView;
    }
}

关于android - 自定义 ArrayAdapter getView 未被调用——为什么不呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397605/

有关android - 自定义 ArrayAdapter getView 未被调用——为什么不呢?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐