I retrieve from database my swimming performance. I would like to change background color of one field according to his value. For example if i swimm 4 laps I want color background. I try this code that set background correctly but text disappears.
String[] columns = new String[] { "swimm_pos", "swimm_date","swimm_lap", "swimm_stroke", "swimm_time", "swimm_media", "swimm_efficiency", "swimm_note" };
int[] to = new int[] { R.id.row_counter, R.id.swimm_date, R.id.swimm_lap, R.id.swimm_stroke, R.id.swimm_time, R.id.swimm_medialap, R.id.swimm_efficiency, R.id.swimm_note};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.contacto_list_item,
cursor,
columns,
to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.swimm_lap)
{
int color = cursor.getInt(columnIndex);
String s = String.valueOf(color);
if (s.equals("4")) {
TextView tv = (TextView)view;
tv.setBackgroundColor(0xFF558866);}
return true;
}
return false;}
});
And is also possible, when lap is equals to 4 set background color of another field, for example in my code: R.id.swimm_pos?
thank you.
Returning true from ViewBinder implies that you are also binding the data to the View.
But in your case you are not setting the text of R.id.swimm_lap.
So add setText before return statement
tv.setText(s);
return true;
Edit:
For the second question suppose you want to change background of R.id.row_counter depending upon swim lap then add
else if (view.getId() == R.id.row_counter){
int color = cursor.getString(cursor.getColumnIndex("swimm_lap"));
if (s.equals("4")) {
view.setBackgroundColor(0xFF558866);
}
}
Solved, here the right code:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.row_counter)
{
int color = cursor.getInt(cursor.getColumnIndex("swimm_lap"));
String s = String.valueOf(color);
if (s.equals("4")) {
TextView tv = (TextView)view;
tv.setBackgroundColor(0xFF558866);
}
return true;
}
return false;}
});
this.setListAdapter(adapter);
datasource.close();
}
Related
I'm a little bit stuck on viewbinders in android
here's my code:
public void displayAllAlerts() {
Cursor mCursor = mDbAdapter.fetchAllAlerts();
//Bind Columns
String[] columns = new String[] {
DbAdapter.KEY_ID,
DbAdapter.KEY_PLACE,
DbAdapter.KEY_LONG,
DbAdapter.KEY_LAT,
DbAdapter.KEY_STATUS
};
int[] to = new int[] {
R.id.txtId,
R.id.txtPlace,
R.id.txtLong,
R.id.txtLat,
R.id.tglBtnAlert
};
mSimpleCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.layout_lvrow,
mCursor,
columns,
to,
0);
ListView lvAlerts = (ListView) findViewById(R.id.lvAlerts);
lvAlerts.setAdapter(mSimpleCursorAdapter);
}
The problem is that 'DbAdapter.key_status' is formatted as an int in my database, but someway I have to change it to a boolean, beacuase it's my status for my togglebutton.
I know i have to use .setViewBinder, but i have no idea were to start.
I tried the following from some tutorials but it does not work:
mSimplecursorAdapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 5) {
String strBool = aCursor.getString(aColumnIndex);
ToggleButton tb = (Togglebutton) aView;
if (strBool=="0") {
tb.setChecked = false;
}else{
tb.setChecked = true;
}
return true;
}
return false;
}
thanks in advance
(also tried already to use developer site of android but it's giving me a real headache)
The code does not work because you must use String.equals() or TextUtils.equals() to compare strings.
To handle boolean columns on SQLite, I usually handle this data as INTEGER with values 1 or 0:
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 5) {
boolean checked = aCursor.getInt(aColumnIndex) == 1;
ToggleButton tb = (Togglebutton) aView;
tb.setChecked(checked);
return true;
}
return false;
}
I'm using multiple selection on the listview in my app which is being populated by db (SimpleCursorAdapter). There's some weird behavior with the listview selection.
If there are more than 7 items in the database, if I select the 1st item in the listview, the 8th item also gets selected even when I'm not selecting the 8th item and vice-versa. If I select the 9th item, the 2nd row gets selected.
What's happening here?
Code:
String[] projection = { ..table_columns..};
String[] from = { table_columns..};
Cursor cursor = contentResolver.query(SomeContentProvider.CONTENT_URI, projection, null, null,
null);
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.color,
R.id.name,
R.id.desc,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.layout_main,
cursor,
from,
to,
0);
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int column) {
int nNameIndex = cursor.getColumnIndexOrThrow(EventsTable.COLUMN_NAME);
if( column == nNameIndex ){
TextView nname = (TextView) view;
String name = cursor.getString(cursor.getColumnIndex(EventsTable.COLUMN_NAME));
String formatted_name = "NAME: " +name;
nname.setText(formatted_name);
return true;
}
return false;
}
});
listView.setAdapter(dataAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
if (!listView.isItemChecked(pos)){
listView.setItemChecked(pos, true);
v.setBackground(getResources().getDrawable(R.drawable.listview_bg_selected));
v.setSelected(true);
} else {
listView.setItemChecked(pos, false);
v.setBackground(getResources().getDrawable(R.drawable.listview_bg));
v.setSelected(false);
}
if (listView.getCheckedItemCount() > 0) {
if (mMode == null) {
mMode = startActionMode(new ActionModeCallback());
} else {
mMode.setTitle(listView.getCheckedItemCount() + " " + "Selected");
}
} else {
if (mMode != null) {
mMode.finish();
}
}
return true;
}
});
I suspect it's because in your bindView of your adapter you are not checking if the item is checked, and then changing the background appropriately.
You experiencing your views being recycled.
So when you scroll, and say item one goes out of view and was selected, the view for item 1 is reused for item 8.
SO add something like this to your view binder
int post = cursor.getPosition();
if (!listView.isItemChecked(pos)){
v.setBackground(getResources().getDrawable(R.drawable.listview_bg_selected));
} else {
v.setBackground(getResources().getDrawable(R.drawable.listview_bg));
}
I have a big problem and I tried to debug it for a long time but I can not get the error.
I have a Listview and load it with pictures, where the Path is saved in the database.
Its all works fine, but I have one Problem:
If I have e.g. 4 items. The first two have pictures and the last 2 not. So the last item in the list always have the picture from the last item which had a picture and I can not figure out why. I looked in the database if it maybe have a Path in it for the last element but its empty.
There is my code:
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this, R.layout.my_listlayout,
MainController.getInstance().getMyCursor(db),
new String[] {"F1", "F2", "F3", "F4"},
new int[] {R.id.imageViewF1, R.id.textViewF2, R.id.textViewF3, R.id.textViewF4}, 0);
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 1) {
ImageView imageView = (ImageView) view;
String Pic = cursor.getString(1);
if (Pic != null) {
// load the picture in the view
imageView.setImageDrawable(...);
}
return true;
}
if (columnIndex == 2) {
//..
}
if (columnIndex == 3) {
//..
}
if (columnIndex == 4) {
//..
}
return false;
}
});
ListView view = (ListView)findViewById(R.id.listViewFirst);
view.setAdapter(adapter);
Maybe I'M doing something wrong in the adapter?
Im trying to change text color in a listview, which is displaying my entries from database. The problem is that it displays invisible text ( or it displays nothing at all. only the lines which are separating my entries on this list) but after clicking on entry it shows me details from this entries. Displaying works good without trying to use 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);
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
int getIndex = cursor.getColumnIndex("colour");
String empname = cursor.getString(getIndex);
tv = (TextView)findViewById(R.id.countryTextView);
tv.setTextColor(Color.WHITE);
if(empname.equals("Green"))
{
tv.setTextColor(Color.WHITE);
return true;
}
return false;
}
};
setListAdapter(conAdapter); // set adapter
((SimpleCursorAdapter) conAdapter).setViewBinder(binder);
How can I change it to work ok ?
You have to add the text to the textview:
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex)
{
((TextView) view).setTextColor(Colors.RED);
((TextView) view).setText(cursor.getString(cursor.getColumnIndex("YOUR-COLUMN-NAME")));
return false;
}
change line
tv = (TextView)view.findViewById(R.id.countryTextView); // <-- add 'view.'
call
conAdapter.setViewBinder(SimpleCursorAdapter.ViewBinder)
setListAdapter(conAdapter); // set adapter
I have an android.R.layout.simple_list_item_multiple_choice with checkboxes an want so initiate some of them.
How can I do that?
I have the following code:
private void fillList() {
Cursor NotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(NotesCursor);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_CHECKED };
int[] to = new int[] {
android.R.id.text1,
android.R.id.text2,
//How set checked or not checked?
};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, NotesCursor,
from, to);
setListAdapter(notes);
}
Put the resource id of your checkbox in your row layout into the to array, corresponding to the NotesDbAdapter.KEY_CHECKED cursor in from array.
Implement a SimpleCursorAdapter.ViewBinder.
Have the ViewBinder.setViewValue() method check for when its called for the NotesDbAdapter.KEY_CHECKED column.
When it is not the KEY_CHECKED column, have it return false the the adapter will do what it normally does.
When it is the KEY_CHECKED column, have it set the CheckBox view (cast required) to checked or not as you wish and then return trueso that adapter won't attempt to bind it itself. The cursor and corresponding column id is available to access query data to determine whether to check the checkbox or not.
Set your ViewBinder in your SimpleCursorAdapter via setViewBinder()
Here's one of my ViewBinder implementations. Its not for checboxes, rather its for doing some fancy formatting of a text view, but it should give you some idea for the approach:
private final SimpleCursorAdapter.ViewBinder mViewBinder =
new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(
final View view,
final Cursor cursor,
final int columnIndex) {
final int latitudeColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_LATITUDE);
final int addressStreet1ColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_ADDRESS_STREET1);
if (columnIndex == latitudeColumnIndex) {
final String text = formatCoordinates(cursor);
((TextView) view).setText(text);
return true;
} else if (columnIndex == addressStreet1ColumnIndex) {
final String text = formatAddress(cursor);
((TextView) view).setText(text);
return true;
}
return false;
}
};