2010-02-14 11 views
9

Estoy escribiendo una aplicación simple para Android.Android ExpandableListActivity y SimpleCursorTreeAdapter?

Tengo 2 tablas: una llamada 'grous' y otra llamada 'group_items'.

Quiero usar una lista ampliable para mostrar los datos de ambas tablas.

¿Cuál es la mejor manera de hacerlo? ¿Es posible mapear datos usando SimpleCursorTreeAdapter? No pude encontrar ningún ejemplo útil.

Vi los ejemplos de cómo crear listas ampliables usando ArrayAdapter, entonces ¿debería convertir primero los datos a una matriz y luego crear una lista ampliable con ella o hay una forma de hacerlo directamente?

No necesito un ejemplo de trabajo completo, solo un consejo sobre cuál es la forma correcta y más eficiente de hacerlo.

Leonti

+0

¿Ha tenido un vistazo aquí: http://developer.android.com/resources/samples/ApiDemos/ src/com/example/android/apis/view/List6.html? –

+0

@Leonti: ¿entonces podrías encontrar una solución? en caso afirmativo, por favor compártelo. será muy útil ya que no hay un buen ejemplo disponible. Gracias –

+0

Estoy haciendo algo similar AQUÍ http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42

Respuesta

19

encontré que la solución más sencilla sería utilizar SimpleCursorTreeAdapter. Aquí es ejemplo de código (partes importantes):

public class ExercisesList extends ExpandableListActivity { 


private ExcercisesDbAdapter mDbHelper; // your db adapter 
private Cursor mGroupsCursor; // cursor for list of groups (list top nodes) 
private int mGroupIdColumnIndex; 
private MyExpandableListAdapter mAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mDbHelper = new ExcercisesDbAdapter(this); 
     mDbHelper.open(); 
     fillData(); 
} 

private void fillData() { 
     mGroupsCursor = mDbHelper.fetchAllGroups(); // fills cursor with list of your top nodes - groups 
     startManagingCursor(mGroupsCursor); 

     // Cache the ID column index 
     mGroupIdColumnIndex = mGroupsCursor 
         .getColumnIndexOrThrow(ExcercisesDbAdapter.KEY_ROWID); 

     // Set up our adapter 
     mAdapter = new MyExpandableListAdapter(mGroupsCursor,this, 

         android.R.layout.simple_expandable_list_item_1, 
         R.layout.exercise_list_row, 

         new String[] { ExcercisesDbAdapter.KEY_TITLE }, // group title for group layouts 
         new int[] { android.R.id.text1 }, 

         new String[] { ExcercisesDbAdapter.KEY_TITLE }, // exercise title for child layouts 
         new int[] { R.id.exercise_title }); 

     setListAdapter(mAdapter); 
} 

// extending SimpleCursorTreeAdapter 
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 

     public MyExpandableListAdapter(Cursor cursor, Context context, 
         int groupLayout, int childLayout, String[] groupFrom, 
         int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
       super(context, cursor, groupLayout, groupFrom, groupTo, 
           childLayout, childrenFrom, childrenTo); 
     } 

     // returns cursor with subitems for given group cursor 
     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
       Cursor exercisesCursor = mDbHelper 
           .fetchExcercisesForGroup(groupCursor 
               .getLong(mGroupIdColumnIndex)); 
       startManagingCursor(exercisesCursor); 
       return exercisesCursor; 
     } 

     // I needed to process click on click of the button on child item 
     public View getChildView(final int groupPosition, 
         final int childPosition, boolean isLastChild, View convertView, 
         ViewGroup parent) { 
       View rowView = super.getChildView(groupPosition, childPosition, 
           isLastChild, convertView, parent); 

       Button details = (Button) rowView.findViewById(R.id.view_button); 

       details.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) { 

           Cursor exerciseCursor = getChild(groupPosition, childPosition); 

           Long exerciseId = exerciseCursor.getLong(exerciseCursor.getColumnIndex(ExcercisesDbAdapter.KEY_ROWID)); 

           Intent i = new Intent(ExercisesList.this, ExerciseView.class); 
           i.putExtra(ExcercisesDbAdapter.KEY_ROWID, exerciseId); 
           startActivity(i); 
         } 
       }); 

       return rowView; 
     } 

} 

} 

Esperamos que sea útil;)

+0

Muy útil, para aquellos de nosotros que tratamos de evitar los proveedores de contenido. –

+0

Señor, ¿podría responder mi pregunta también? Http://stackoverflow.com/questions/29457424/expandablelistview-extends-simplecursoradapter-to-populate-from-sqlite – silverFoxA