I have an Activity (not a ListActivity, although I should probably convert my code into one since it’s only displaying a ListView). In this ListView I have a setup a layout for the rows, and it contains two TextView and one CheckBox. The Layout is shared by another Activity, so I have set CheckBox.visibility="GONE" so the activities can enable them on their own if they need the checkboxes.
I have a SimpleCursorAdapter for my ListView. In this adapter I override setViewValue() so I can enable the checkbox, like this:
adapter = new SimpleCursorAdapter(this, R.layout.location_browser_listview_relative, cursor, FROM, TO);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.checkBox) {
// Enable the star-checkbox
ldb.isFavourite(cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID)));
view.setVisibility(View.VISIBLE);
((TextView)view).setText("");
return true;
}
return false;
} // setViewValue
}); // setViewBinder
furthermore I have setup an onClickListener() because the user is supposed to click a list element (a whole row). The listener goes like this:
listItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final int numItems = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_ITEMS));
String foo = String.format(TAG + "Clicked: %d", numItems);
Log.i(LOG_TAG, foo);
AlertDialog.Builder dialog = new AlertDialog.Builder(StoreBrowser.this);
dialog.setMessage("Vill du bevaka?");
dialog.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO: Save to DB here
System.out.println("Hello from onClick");
ldb.addFavourite(ID);
}
}); // setPositiveButton
dialog.setNegativeButton("Nej", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Clicked no", Toast.LENGTH_SHORT).show();
}
}); // setNegativeButton
dialog.show();
} // onItemClick
}); // setOnItemClickListener
Questions:
- In setViewValue(), the code
ldb.isFavourite()is issued three times according to Logcat (I use Log.d() in the method). Why is it called three times? There’s a SQL-query running in it, and I suspect it will be quite expensive running it three times for each listview row.
2a. How can I intercept a click on the checkbox? I need to toggle a boolean variable when the user toggles the checkbox. The onClick() only fires when the user clicks the whole listview row, but when the checkbox is clicked nothing happens.
2b. Is there a way to collect all checkbox info at the same time instead of toggling a boolean when user clicks? I mean, when the user is done selecting, is there a way to go through all the listelements one by one in a loop and fetch the click-value?
Bonus question: I suspect I should rewrite this code and manually inflate the listview and its elements, so if anyone can point me to a good resource describing this kind of work I’d be grateful. Google’s API-docs aren’t very helpful.
@ 1. How many rows does your cursor iterate over? – I suspect it might be 3.
@ 2b. Not quite sure about the
ListView, but have a look atViewGroup.getChildAt()to iterate through theListView‘s children and atView.findViewById()to locate your desired child view inside each child of the ListView.