SimpleCursorAdapter.ViewBinder文本未显示

时间:2013-03-09 17:19:51

标签: android simplecursoradapter android-viewbinder

我遇到了问题而无法找到解决方案。

我有一个数据库,我使用 SimpleCursorAdapter 加载一个列表视图。

我想根据数据库中列的值(“incOrExp”)更改文本的颜色。

这是我的清单项目:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="#afeeee" >

<TextView 
android:id="@+id/incomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingTop="5dp"
android:paddingLeft="20dp"
android:paddingBottom="2dp"
android:textSize="20sp"
android:textColor="#b22222" />

<TextView
android:id="@+id/amountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/currencyTextView"
android:paddingTop="10dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:textColor="#0000cd"
android:textSize="20sp" />

<TextView
android:id="@+id/currencyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:textColor="#0000cd"
android:textSize="20sp"
android:text="€" />

<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/incomeTextView"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:paddingTop="2dp"
android:paddingLeft="20dp"
android:paddingBottom="5dp"
android:textColor="#0000cd"
android:textSize="12sp" />

</RelativeLayout>

我想使用SimpleCursorAdapter.ViewBinder()

更改(TextView) amountTextView 的颜色

这是我的代码:

    String[] from = new String[] {"inCategory","inAmount","inDateFormat"}; 
    int[] to = new int[] { R.id.incomeTextView, R.id.amountTextView, R.id.dateTextView}; 

    incomeAdapter = new SimpleCursorAdapter (this, R.layout.income_list_item, null, from, to);

    incomeAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          if (view.getId() == R.id.amountTextView)
            { 
              int incOrExpIndex = cursor.getColumnIndex("incOrExp");
              int incOrExp = cursor.getInt(incOrExpIndex);
              if (incOrExp==1) {
              TextView tvColor = (TextView)view;
              tvColor.setTextColor(Color.YELLOW);
              }
              else {
                  TextView tvColor = (TextView)view.findViewById(R.id.amountTextView);
                  tvColor.setTextColor(Color.RED);
              }
             return true;

        }
          return false;}

    });

    incomeListView.setAdapter(incomeAdapter); 

我的错在哪里?

提前谢谢..

1 个答案:

答案 0 :(得分:3)

我找到了答案......

我必须设置颜色,然后我必须设置文本....

incomeAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          if (view.getId()==R.id.amountTextView)
            { 
              int incOrExpIndex = cursor.getColumnIndex("incOrExp");
              int incOrExp = cursor.getInt(incOrExpIndex);

            switch(incOrExp) {
                case 0: 
                    TextView tv0 = (TextView)view;
                    tv0.setTextColor(Color.BLUE);
                    tv0.setText(cursor.getString(cursor.getColumnIndex("inAmount")));
                    break;
                case 1: 
                    TextView tv1 = (TextView)view;
                    tv1.setTextColor(Color.RED);
                    tv1.setText(cursor.getString(cursor.getColumnIndex("inAmount")));
                    break;
              }
             return true;

        }
          return false;}
    });

我要感谢那些试图解决这个问题的人......