Hai ragione, scusami
queste sono le ultime versioni:
package com.turniDB;
import java.util.ArrayList;
import turni.db.tab.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class Gennaio extends Activity {
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private ArrayList<String> turno_id = new ArrayList<String>();
private ArrayList<String> turno_mEse = new ArrayList<String>();
private ArrayList<String> turno_gIorno = new ArrayList<String>();
private ArrayList<String> turno_tUrno = new ArrayList<String>();
private ArrayList<String> turno_oRe = new ArrayList<String>();
private ArrayList<String> turno_totoRe = new ArrayList<String>();
private ListView userList;
private AlertDialog.Builder build;
private int totore;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row2);
userList = (ListView) findViewById(R.id.List);
mHelper = new DbHelper(this);
//Aggiunta nuovi record
findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
AddActivity.class);
i.putExtra("Aggiorna", false);
startActivity(i);
}
});
//Aggiornamento dati
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(),
AddActivity.class);
i.putExtra("TurnoID", turno_id.get(arg2));
i.putExtra("Mese", turno_mEse.get(arg2));
i.putExtra("Giorno", turno_gIorno.get(arg2));
i.putExtra("Turno", turno_tUrno.get(arg2));
i.putExtra("Ore", turno_oRe.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});
//Click lungo per cancellare
userList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
build = new AlertDialog.Builder(Gennaio.this);
build.setTitle("Cancella " +turno_mEse.get(arg2) +turno_gIorno.get(arg2) +turno_tUrno.get(arg2) + " " +turno_oRe.get(arg2));
build.setMessage("Vuoi cancellare ?");
build.setPositiveButton("Si",
new DialogInterface.OnClickListener() {
@SuppressLint("ShowToast")
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(
getApplicationContext(),
turno_mEse.get(arg2) +turno_gIorno.get(arg2) +turno_tUrno.get(arg2) + " " +turno_oRe.get(arg2) +" è stato cancellato", 3000).show();
dataBase.delete(
DbHelper.TURNI_TABLE,
DbHelper.TURNO_ID + "="
+ turno_id.get(arg2), null);
displayTurni();
dialog.cancel();
}
});
build.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});
}
@Override
protected void onResume() {
displayTurni();
super.onResume();
}
/**
* Visualizza dati da SQLite
* @return
*/
private Object displayTurni() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DbHelper.TURNI_TABLE+" WHERE MESE = 'Gennaio'", null);
turno_id.clear();
turno_mEse.clear();
turno_gIorno.clear();
turno_tUrno.clear();
turno_oRe.clear();
if (mCursor.moveToFirst()) {
do {
turno_id.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.TURNO_ID)));
turno_mEse.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.MESE)));
turno_gIorno.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.GIORNO)));
turno_tUrno.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.TURNO)));
turno_oRe.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.ORE)));
Cursor cursor = dataBase.rawQuery(
"SELECT SUM(ore) AS totore FROM "+DbHelper.TURNI_TABLE,
new String[0]);
int colIndex = cursor.getColumnIndex("totore");
if (colIndex == -1)
return null;
else
cursor.moveToFirst();
totore = cursor.getInt(colIndex);
System.out.println("La somma è "+totore);
Integer.toString(totore);
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(Gennaio.this,turno_id, turno_mEse, turno_gIorno, turno_tUrno, turno_oRe, turno_totoRe);
userList.setAdapter(disadpt);
mCursor.close();
return disadpt;
}
}
Display Adapt
import java.util.ArrayList;
import turni.db.tab.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import turni.db.tab.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> mese;
private ArrayList<String> giorno;
private ArrayList<String> turno;
private ArrayList<String> ore;
private ArrayList<String> totore;
public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> mese, ArrayList<String> giorno, ArrayList<String> turno, ArrayList<String> ore, ArrayList<String> totore) {
this.mContext = c;
this.id = id;
this.mese = mese;
this.giorno = giorno;
this.turno = turno;
this.ore = ore;
this.totore = totore;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.main2, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_mEse = (TextView) child.findViewById(R.id.txt_mEse);
mHolder.txt_gIorno = (TextView) child.findViewById(R.id.txt_gIorno);
mHolder.txt_tUrno = (TextView) child.findViewById(R.id.txt_tUrno);
mHolder.txt_oRe = (TextView) child.findViewById(R.id.txt_oRe);
mHolder.txt_totoRe = (TextView) child.findViewById(R.id.txt_totoRe);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_mEse.setText(mese.get(pos));
mHolder.txt_gIorno.setText(giorno.get(pos));
mHolder.txt_tUrno.setText(turno.get(pos));
mHolder.txt_oRe.setText(ore.get(pos));
mHolder.txt_totoRe.setText(totore.get(pos));
return child;
}
public class Holder {
TextView txt_id;
TextView txt_mEse;
TextView txt_gIorno;
TextView txt_tUrno;
TextView txt_oRe;
TextView txt_totoRe;
}
e log
[02-17 08:19:20.885: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:20.935: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:21.155: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:21.206: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:21.664: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:21.734: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:21.784: D/gralloc_goldfish(930): Emulator without GPU emulation detected.
02-17 08:19:24.974: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:25.064: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:25.474: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:25.504: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:29.974: D/InputEventConsistencyVerifier(930): KeyEvent: ACTION_UP but key was not down.
02-17 08:19:29.974: D/InputEventConsistencyVerifier(930): in android.widget.EditText@412e8760
02-17 08:19:29.974: D/InputEventConsistencyVerifier(930): 0: sent at 272577000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=272577, downTime=272577, deviceId=0, source=0x101 }
02-17 08:19:37.945: D/dalvikvm(930): GC_CONCURRENT freed 126K, 3% free 9347K/9543K, paused 26ms+7ms
02-17 08:19:40.404: I/System.out(930): La somma è 8
02-17 08:19:40.484: D/AndroidRuntime(930): Shutting down VM
02-17 08:19:40.484: W/dalvikvm(930): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-17 08:19:40.564: E/AndroidRuntime(930): FATAL EXCEPTION: main
02-17 08:19:40.564: E/AndroidRuntime(930): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-17 08:19:40.564: E/AndroidRuntime(930): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-17 08:19:40.564: E/AndroidRuntime(930): at java.util.ArrayList.get(ArrayList.java:304)
02-17 08:19:40.564: E/AndroidRuntime(930): at com.turniDB.DisplayAdapter.getView(DisplayAdapter.java:83)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.ListView.onMeasure(ListView.java:1155)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-17 08:19:40.564: E/AndroidRuntime(930): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.View.measure(View.java:12723)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.os.Looper.loop(Looper.java:137)
02-17 08:19:40.564: E/AndroidRuntime(930): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-17 08:19:40.564: E/AndroidRuntime(930): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 08:19:40.564: E/AndroidRuntime(930): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 08:19:40.564: E/AndroidRuntime(930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-17 08:19:40.564: E/AndroidRuntime(930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-17 08:19:40.564: E/AndroidRuntime(930): at dalvik.system.NativeStart.main(Native Method)
02-17 08:19:40.874: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:40.894: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
02-17 08:19:41.174: I/dalvikvm(930): threadid=3: reacting to signal 3
02-17 08:19:41.184: I/dalvikvm(930): Wrote stack traces to '/data/anr/traces.txt'
]
Una domanda, la texview che deve visualizzare il totale, si trova sempre in main 2 ma fuori della listview, la mia domanda è:
Non dovrei inviare l'out direttamente alla textview senza passarla alla arraylist? o sbaglio?
in caso, non dovrei dargli indicazioni dopo l'istruzione select sum?