Child pages
  • CursorAdapter kinyerése ListActivity-ből
Skip to end of metadata
Go to start of metadata

Sziasztok,

egy ListActivity ős view-m van:

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

Ez az ActivitiList őse lesz több hasonló képernyőnek, a renderlistview()-et ők maguknak valósítják meg, SimpleCursorAdapter-rel pl.:

public class ActivityProductList extends ActivityList {
 @Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
  renderListView( null );
 }
 @Override
public void renderListView( String filter ) {
 ...
 SimpleCursorAdapter sca = new SimpleCursorAdapter( this, listItemId, mCursor, from, to );
 setListAdapter( sca );

}

A listview szépen működik.

A felhasználó teszi amit akar, aztán megnyomja a KÉSZ gombot. Ez a gomb az ős activity-ben van, amiben a listview is. Az eventhandlere:

protected void getCheckedItems() {
 SimpleCursorAdapter adapter = (SimpleCursorAdapter)this.getListAdapter(); // <<<<
 Cursor cursor = adapter.getCursor();
 ...
}

A probléma az, hogy kommenttel jelölt sorban elszáll a program ismeretlen exceptionnel és nem tom miért?????

Van valakinek valami 5lete?

Előre is köszönöm! (thumbs up)

 

 

      
      
Page viewed times
#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels

7 Comments

    1. Android 2.1, SE XPERIA 8

      A logban sajnos nem látok semmi erre utalót (log collector), debuggolom a programot. A stackoverflow-ra írtam egy bővebb körülírását a problémának, azt ideteszem:

      <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() {
       
      SimpleCursorAdapter adapter = (SimpleCursorAdapter)this.getListAdapter();  // <<<<
       
      Cursor cursor = adapter.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 );
       
      }
      }

      The concept is: get the selected item's ids.

       

      However, in the line what I marked by comment the program crashes with error:eclipse screenshot part

      The this.getListAdapter() gives me a CursorAdapter than the cast will crashes.

      My conception is: ActivityList (this) has getListAdapter() method which gives Listadapter. Because the ListActivity's listadapter was set by SimpleCursorAdapter, I use typecasting on it to SimpleCursorAdapter. After that the adapter has getCursor() method which gives the necessary cursor. Maybe it's wrong conception?


      Az érdekesség az (legalábbis számomra (wink)) hogy a 

      this.getListAdapter()az egy SimpleCursorAdapter, mégsem lehet kásztolás nélkül értékül adni az adapter változónak.

       Jó mondjuk továbbra is lelkes kezdő vagyok...

      Volna valami ötleted?

      1. Itt a log, írja is benne, hogy ClassCastException... valószínűleg mégsem SimpleCursorAdapter az adott példány, írasd ki valahova, hogy mi a pontos típusa:

        String className = this.getListAdapter().getClass().getName();
        Log.e("MyProject", className);

        Akkor kiderül, hogy mit kapsz, mert lehet, hogy nem SimpleCursorAdapter (ami egy leszármazott osztály), hanem annak egy őse, vagy más ágon leszármazottja, és meg kell keresni a közös őst, például CursorAdapter esélyes lehet.

        1. a kód:

          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( "NanCal", exception.getMessage() + "::" + exception.toString() );
          }

          ez kerül logba a kód kiegészítésével:

          08-24 15:22:11.110 E/NanCal  ( 3920): android.widget.SimpleCursorAdapter
          08-24 15:22:11.110 E/NanCal  ( 3920): android.widget.SimpleCursorAdapter::java.lang.ClassCastException: android.widget.SimpleCursorAdapter
          1. Hmm... ez fura. Egyelőre nincs ötletem, de még este megnézem.

            1. Szia,

              sikerült ránézned? Teljesen nem értem, áll a dolog... A kurzorra sajna szükségem van, mert a lista lehet korábban szűrt sql eredménye is, amit a kurzor visszaadna. Így a 

              cursor.moveToPosition( selectedPosition );

              miatt kell ez a dudva hibás sor...

              Köszi

              1. Sajnos nincs ötletem. Mennie kellene ránézésre, nem tudom miért nem működik. (sad)