【问题标题】:Using SimpleCursorAdapter.ViewBinder to change color in a listView. android使用 SimpleCursorAdapter.ViewBinder 更改 listView 中的颜色。安卓
【发布时间】:2012-12-26 11:08:57
【问题描述】:

我试图更改 ListView 中文本的颜色,所以我创建了 ViewBinder,但我的文本仍然是白色的,没有任何变化,我能用它做什么?

// map each name to a TextView
    String[] from = new String[] { "event" };
    int[] to = new int[] { R.id.countryTextView };
    conAdapter = new SimpleCursorAdapter(Clock.this, R.layout.day_plan, null, from, to);
    setListAdapter(conAdapter); // set adapter

    SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex){    
            int getIndex = cursor.getColumnIndex("colour");
            String color = cursor.getString(getIndex);
           tv = (TextView)findViewById(R.id.countryTextView);

            tv.setTextColor(Color.BLUE);

           if(color.equals("Green"))
            {                   
                tv.setTextColor(Color.rgb(0,255,0));
                return true;
            } 
            return false;          
        }};

【问题讨论】:

    标签: android listview android-listview android-viewbinder


    【解决方案1】:

    你打电话给conAdapter.setViewBinder(SimpleCursorAdapter.ViewBinder)了吗?也就是说,您确定这些方法实际上被调用了吗?

    同时更改其中一行:

           tv = (TextView)view.findViewById(R.id.countryTextView); // <-- add 'view.'
    

    【讨论】:

    • 我应该如何以及在哪里调用它?
    • 在您的代码后面添加conAdapter.setViewBinder(binder);。尝试输入 Log.d() 以验证 setTextColor() 确实被调用了。
    • 我收到错误:01-11 21:15:07.632: E/AndroidRuntime(1144): FATAL EXCEPTION: main 01-11 21:15:07.632: E/AndroidRuntime(1144): java. lang.IllegalStateException:无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标。
    • 这发生在哪一行代码?听起来您得到的Cursor 无效;哪一行代码返回您尝试在适配器上设置的 Cursor
    • 你能发布你的 day_plan.xml 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2011-06-05
    • 2017-02-04
    相关资源
    最近更新 更多