-1

Ive been trying to create a simplistic database on Android that has a table that gets three values,one for id, one for event desc. and the other for time which is on string format. problem is that it crashes for no reason and i cant find why. here is MainActivity:

public class MainActivity extends ListActivity {

private static final int MENU_EDIT = Menu.FIRST+1;
private static final int MENU_REMOVE = Menu.FIRST+2;
ArrayList <String> Events = new ArrayList<String>();
ArrayList <Integer> ids = new ArrayList<Integer>();
ListView lview = (ListView)findViewById(R.id.listview);

DBHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // registerForContextMenu(getListView());

    db = new DBHandler(this);

    // db.addInfo(new Day_Info("stef", "123"));


    loadList();



}

private void loadList() {

    try{
        Events.clear();
        ids.clear();
        for(Day_Info e:db.getAllInfo()){
            Events.add(e.getEvents()+", "+e.getTime());
            ids.add(e.getId());
        }
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_2,Events);
        lview.setAdapter(adapter);
    }
    catch(Exception e){
        Toast.makeText(this, "Problem Loading List", Toast.LENGTH_LONG).show();
    }




}

AND HERE is my database class:

class DBHandler extends SQLiteOpenHelper {



private static final String TABLE_NAME = "INFO";

private static final String DATABASE_NAME = "Schedule";

private static final String EVENT_DB ="event";

private static final String TIME_DB = "time";


public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, 1);

    //Log.d("Database", "Database created");


}

@Override
public void onCreate(SQLiteDatabase db) {
    String TABLE_CREATION = "CREATE TABLE "+ TABLE_NAME +" (_id INTEGER PRIMARY KEY AUTOINCREMENT, EVENT TEXT not null,TIME TEXT not null)";
    db.execSQL(TABLE_CREATION);

    Log.d("Database", "Tables created");
}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
                      int newVersion) {

}


void addInfo(Day_Info info){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(EVENT_DB, info.getEvents());
    cv.put(TIME_DB, info.getTime());

    db.insert(TABLE_NAME, null, cv);
    db.close();
}

public ArrayList<Day_Info> getAllInfo(){
    ArrayList<Day_Info> List = new ArrayList<Day_Info>();

    String selectQuery = "SELECT * FROM "+TABLE_NAME;

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);

    if(cursor.moveToFirst()){
        do{
            Day_Info info= new Day_Info();
            info.setId(Integer.parseInt(cursor.getString(0)));
            info.setEvents(cursor.getString(1));
            info.setTime(cursor.getString(2));
            List.add(info);
        }while(cursor.moveToNext());
        cursor.close();
    }
    return List;

}

}

Stephanos B.
  • 340
  • 3
  • 15

2 Answers2

0

Your coloumn at position 0 is an integer type

`_id INTEGER PRIMARY KEY AUTOINCREMENT`

But you are trying to fetch it as string

info.setId(Integer.parseInt(cursor.getString(0)));

instead use

info.setId(cursor.getInt(0));

Without logcat, i think this is the error you are facing. if you show your logs i will help more to determine the issue

Nitesh
  • 3,868
  • 1
  • 20
  • 26
0

you also declare _id as integer on need to convert cursor position (0) into integer put cursor.getInt(0)