【问题标题】:ViewBinder modifies only one item in all ListView rowsViewBinder 只修改所有 ListView 行中的一项
【发布时间】:2012-02-22 02:23:17
【问题描述】:

我有一个ListView,它由SimpleCursorAdapter 填充,每行包含3 个不同的TextViews。我只想用ViewBinder (R.id.text65) 修改所有行的TextViews 之一,但是它会不断更新每行的所有3 个TextViews。这是我的代码:

cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            sign = (TextView) view;
            SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            String currency1 = currency.getString("Currency", "$");
                    sign.setText(currency1);

                    return true;
        }
    });

附:我尝试了(TextView) findViewById(R.id.text65);,得到了Force close

【问题讨论】:

    标签: android android-listview textview simplecursoradapter android-viewbinder


    【解决方案1】:

    解决方案 1:

    你应该检查viewbinder中的列索引:

           public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
               if (columnIndex == cursor.getColumnIndexOrThrow(**??**)) // 0 , 1 , 2 ?? 
                {
                   sign = (TextView) view;
                   SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    String currency1 = currency.getString("Currency", "$");
                        sign.setText(currency1);
    
                        return true;
                 }
                 return false;
            }
    

    注意,列索引是货币的 DBcolumn 索引/您的数据源中的列索引。

    解决方案 2:

    您可能正在为listview 中要绑定的字段定义一个int[],例如:

                // and an array of the fields we want to bind those fields to
        int[] to = new int[] { R.id.field1, R.id.field2, R.id.Currency };
    
        SimpleCursorAdapter entries = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
    

    ...有条件的,您可以简单地传递0,而不是您不想绑定/显示的字段的布局ID。

                int[] to = new int[] { 0, 0, R.id.Currency };
    

    这样只会绑定货币字段。


    另外,您获得 force close 的原因是,从技术上讲,您的 contentView 中没有 single text65 em>很多。您无法从主布局级别访问它。它仅在单行范围内是唯一的。


    更新:

    解决方案 3:

    查看ViewBinder中视图的id

        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            int viewId = view.getId();
            Log.v("ViewBinder", "columnIndex=" + columnIndex + " viewId = " + viewId);
            if(viewId == R.id.text65)
            {
                sign = (TextView) view;
                SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                String currency1 = currency.getString("Currency", "$");
                sign.setText(currency1);
    
                return true;
             }
             return false;
         }
    

    你可以试试这个吗?

    有用的提示:您可以使用Log.v 来检查代码中的某些值,而无需对其进行调试。

    希望对你有帮助。

    【讨论】:

    • 这是非常好的信息,谢谢,但是让我更具体地了解一下情况。在我的列表视图中,每一行都包含一个日期、一个费用和一个货币符号,该费用可以在设置中更改。 (例如 [6 月 19 日 $102.32])。如果我的货币符号位于我的数据库中,这些解决方案将非常有用,但它只是位于 sharedpreferences 中的一个简单值,我希望在每一行中复制它。再次感谢您的宝贵时间
    • 所以发生的事情是我的列表视图行显示为 [$ $ $] 因为它改变了所有 3 个文本视图。如果您对此有解决方案,我将不胜感激!
    • hmm.. 你应该可以使用第一个解决方案。代替cursor.getColumnIndexOrThrow(...) 尝试硬编码值(0、1、2)并进行简单测试,看看您是否可以获得货币列集。
    • 我尝试了 0-10 的数字,但它一直返回 false....啊,我希望我很聪明并且知道这些东西
    • 这很奇怪.. 我建议您尝试另一种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2013-06-12
    • 2011-12-28
    相关资源
    最近更新 更多