I use the most simpliest way in layout to define listview:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true" />
Since I need a checked list I use CheckedTextView
in listitem template. The user can check as many items in list as he want. It's working fine.
I have a ListActivity
root view:
public abstract class ActivityList extends ListActivity {
protected ListView listView;
public abstract void renderListView( String filter );
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.commonlist );
listView = getListView();
}
}
It is abstract, because ActivitiList
will be the parent for other view classes, like products, clients, etc. The abstract renderlistview() will be implemented by children with SimpleCursorAdapter
, eg.:
public class ActivityProductList extends ActivityList {
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
...
renderListView( null );
}
@Override
public void renderListView( String filter ) {
listView.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE );
...
SimpleCursorAdapter sca = new SimpleCursorAdapter( this, R.layout.productlistitemchecked, mCursor, from, to );
setListAdapter( sca );
}
}
The ActivityProductList is working fine. The user does it all he want, then clicks on the FINISH button defined and implemented in parent class, where the listview is, too. His event handler is as follows:
protected void getCheckedItems() {
try {
String className = this.getListAdapter().getClass().getName();
Log.e("NanCal", className);
Class c = this.getListAdapter().getClass();
ListAdapter la = this.getListAdapter();
SimpleCursorAdapter ca = ( SimpleCursorAdapter )la;
Cursor cursor = ca.getCursor();
SparseBooleanArray selectedItems = listView.getCheckedItemPositions();
for( int i = 0; i < selectedItems.size(); i++ ) {
int selectedPosition = selectedItems.keyAt( i );
cursor.moveToPosition( selectedPosition );
long rowId = ca.getItemId( selectedPosition );
Log.d( "", "row id: " + rowId );
}
} catch( Exception exception ) {
Log.e( "NanCalc", exception.getMessage() + "::" + exception.toString() );
}
}
The concept is: get the selected item's ids.
However, in the line what I marked by comment the program crashes with error:
The this.getListAdapter() gives me a CursorAdapter
than the cast will crashes.
Could somebody help me please?
I solved the problem with sword...
I've tried to access the SimpleCursorAdapter from parent class (ActivityList). This way didn't work. Therefore I§ve changed the base class to abstract and add an abstract method which will return the selected row id's. The abstract method is implemented in child class (ActivityProductList).
I know this solution doesn't give mention to the original problem. But it works...
Thank you all responses.