0

我一直在处理一个问题,我找不到解决方案。

我有一个数据库,我使用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); 

我的错在哪里?

先感谢您..

4

1 回答 1

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

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

于 2013-03-09T18:01:40.620 回答