ViewBinder仅修改ListView所有行中的一项

发布于 2025-01-08 05:27:45 字数 823 浏览 3 评论 0原文

我有一个 ListView,它由一个 SimpleCursorAdapter 填充,每行包含 3 个不同的 TextView。我只想使用 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;
        }
    });

PS 我尝试了 (TextView) findViewById(R.id.text65);​​ 并且我得到了 强制关闭

I have a ListView that's populated from a SimpleCursorAdapter each row containing 3 different TextViews. I only want to modify one of the TextViews for all rows with a ViewBinder (R.id.text65), however it keeps updating all 3 TextViews of every row. Here's my code:

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

P.S. I tried (TextView) findViewById(R.id.text65); and I got a Force close.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2025-01-15 05:27:45

解决方案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;
        }

注意,列索引是货币的DB列索引/您的任何内容中的列索引数据源。

解决方案 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 };

这样只有货币字段会被绑定。


另外,您强制关闭的原因是,从技术上讲,您的contentView中没有单个 text65很多。您无法从主布局级别访问它。它仅在单行范围内是唯一的。


更新:

解决方案 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 检查代码中的某些值,而无需调试它。

希望有帮助。

Solution 1:

You should check the column index in the 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;
        }

Note, the column index, is the DBcolumn index of the currency / the index of the column in whatever is your data source.

Solution 2:

You are probably defining an int[] for the fields to bind to in your listview for example :

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

... Conditionally, you can simply pass 0 instead of the layout ids of the fields that you donT want to be bound / shown.

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

This way only the Currency field will be bound.


Also, the reason you get the force close is because, technically, there is no single text65 in your contentView but many. You cannot access it from the main layout level. It is unique only in the scope of a single row.


Update :

Solution 3:

Check the id of the view in the ViewBinder

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

Could you try this?

Useful hint: you can use the Log.v to check certain values in your code without having to debug it.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文