简体   繁体   English

SimpleCursorAdapter.ViewBinder文本未显示

[英]SimpleCursorAdapter.ViewBinder text not showing

I have been stacked at a problem and i can not find the solution. 我被堆放在一个问题上,我找不到解决方案。

I have a database and i load a listview using SimpleCursorAdapter . 我有一个数据库,并使用SimpleCursorAdapter加载了一个listview。

I want to change the color of a text depending on the value of a column in database ("incOrExp"). 我想根据数据库中列的值(“ incOrExp”)更改文本的颜色。

This is my list item: 这是我的清单项目:

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

I want to change the color of the (TextView) amountTextView using SimpleCursorAdapter.ViewBinder() 我想使用SimpleCursorAdapter.ViewBinder()更改(TextView) amountTextView的颜色

This is my code: 这是我的代码:

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

Where is my fault? 我的错在哪里

Thank you in advance.. 先感谢您..

I have found the answer... 我找到了答案...

I had to set the color and after that i had to setText too.... 我必须设置颜色,之后我也必须设置setText。

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

I want to thank people who have tried to solve this problem... 我要感谢尝试解决此问题的人们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SimpleCursorAdapter.ViewBinder更改Listview中的文本颜色 - Changing text color in Listview using SimpleCursorAdapter.ViewBinder 使用SimpleCursorAdapter.ViewBinder显示数据库中的文本和图像 - Display text and image from database with SimpleCursorAdapter.ViewBinder Android - SimpleCursorAdapter.ViewBinder - 设置背景颜色 - Android - SimpleCursorAdapter.ViewBinder - Set background color Android SimpleCursorAdapter.ViewBinder不更新绑定的TextView - Android SimpleCursorAdapter.ViewBinder not updating bound TextView 使用SimpleCursorAdapter.ViewBinder更改TextView的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of TextView 使用SimpleCursorAdapter.ViewBinder更改视图的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of View SimpleCursorAdapter.ViewBinder-不同地应用于每一行(ListFragment) - SimpleCursorAdapter.ViewBinder - Apply differently to each row (ListFragment) 使用SimpleCursorAdapter.ViewBinder更改listView中的颜色。 安卓 - Using SimpleCursorAdapter.ViewBinder to change color in a listView. android 具有SimpleCursorAdapter的android ViewBinder - android ViewBinder with SimpleCursorAdapter LoadManager,SimpleCursorAdapter,ViewBinder-没有URI? - LoadManager, SimpleCursorAdapter, ViewBinder - no URI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM