Voglio popolare una ListView con 2 Array. Il primo array è visualizzato nella prima colonna della Listview; il secondo Array, nella seconda colonna, che viene estratto dal database e deve posizionarsi sulla stessa riga.
(tipo un'agenda)
Ecco quello che ho fatto finora, e questo è l'errore che ho.
03-20 23:12:00.918 8517-8517/com.poliveira.apps.materialtests E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at appuntamenti.Elenco_Fragment$1.getView(Elenco_Fragment.java:164)
at android.widget.AbsListView.obtainView(AbsListView.java:2696)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
at android.widget.ListView.onMeasure(ListView.java:1165)
at android.view.View.measure(View.java:16271)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:16271)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16271)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:16271)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16271)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4945)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2501)
at android.view.View.measure(View.java:16271)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2182)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1330)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1541)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1227)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4995)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
static final String[] orari = new String[] {
"8:00", "8:30", "9:00", "9:30", "10:00",
"10:30", "11:00", "11:30", "12:00", "12:30",
"13:00", "13:30", "14:00", "14:30", "15:00",
"15:30", "16:00", "16:30", "17:00", "17:30",
"18:00", "18:30", "19:00", "19:30", "20:00"};
private void prova(){
SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();
final List<Dettaglio> dettagli = new ArrayList<Dettaglio>();
for (int i=1; i<=orari.length; i++){
String tabella_op = "SELECT ........";
Cursor cur = db.rawQuery(tabella_op, null);
while (cur.moveToNext()) {
Dettaglio d = new Dettaglio();
d.id = cur.getString(0);
d.hour= cur.getString(1);//esempio 1 solo record in memoria 10:30
d.nome = cur.getString(2);
d.cognome = cur.getString(3);
dettagli.add(d);
}
cur.close();
}
db.close();
ListAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.appuntamenti_row, R.id.timeText, orari){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
String ore = orari[position];
TextView tvHour = (TextView) row.findViewById(R.id.timeText);
tvHour.setText(ore);
TextView tvA;
tvAppuntamento = (TextView) row.findViewById(R.id.eventText);
Dettaglio d = dettagli.get(position);
tvA.setText(d.nome+" "+d.cognome);
return row;
}
};
list.setAdapter(adapter);
}