问题
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.
I want to change the color of a text depending on the value of a column in database ("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()
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..
回答1:
I have found the answer...
I had to set the color and after that i had to setText too....
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...
来源:https://stackoverflow.com/questions/15313643/simplecursoradapter-viewbinder-text-not-showing