lundi 22 décembre 2014

Android SQL Database issue

I am trying to create a simple app as of right now but for some reason either the app is not adding my athlete to the database or is not incrementing the id. I cannot tell. My toString() for my athlete class works good and prints what is in the athlete object but everytime I try to print out my database for an easy read, it prints no values. I believe an issue may be with the addAthlete function in the fragmentB class but am not sure


I am not getting any errors but it is frustrating that it wont add my athlete to the db. Any help would be great! I may just be making a simple mistake but idk.


Main Activity which includes both fragments:



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

initializeVariables();

AthleteCreation athleteCreation = new AthleteCreation();
ListFragment listFragment = new ListFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.addAthleteFragment, athleteCreation, "AthleteCreation");
transaction.add(R.id.listFragment, listFragment, "listFragment");
transaction.commit();
}

public void initializeVariables(){

}

@Override
public void send(Athlete athlete) {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
ListFragment listFragment = (ListFragment) manager.findFragmentById(R.id.listFragment);
listFragment.addAthlete(athlete);
}
}


Add Athlete Fragment(part):



public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

initializeVariables();

btnAdd.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnAdd:

athlete.setFirstName(editFirstName.getText().toString());
athlete.setLastName(editLastName.getText().toString());
athlete.setAge(Integer.parseInt(editAge.getText().toString()));
athlete.setGender(genderSelected);
athlete.setEvent(eventSelected);
athlete.setTier(Integer.parseInt(editTier.getText().toString()));

Toast.makeText(getActivity(), athlete.toString(), Toast.LENGTH_LONG).show();
communicator = (Communicator) getActivity();
communicator.send(athlete);
}
}


}


Fragment B which receives info from addAthlete Fragment:



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
db = new MySQLiteHelper(getActivity(), "AthletesDB", null, 1);
db.deleteAllAthletes();
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// After activity is created in case fragment is created before activity
initializeVariables();
}
public void initializeVariables(){
displayTable = (TextView) getView().findViewById(R.id.displayTable);
}

public void addAthlete(Athlete athlete){
// Add athlete to listview(database)
firstName = athlete.getFirstName();
lastName = athlete.getLastName();
age = athlete.getAge();
gender = athlete.getGender();
event = athlete.getEvent();
tier = athlete.getTier();
db.addAthlete(athlete);
displayTable.setText(db.showTable());

}


Database:


public class MySQLiteHelper extends SQLiteOpenHelper{



private static final String DATABASE_NAME = "AthletesDB";
private static final int VERSION = 1;

private static final String TABLE_NAME = "Athletes";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_FIRST_NAME = "First_Name";
private static final String COLUMN_LAST_NAME = "Last_Name";
private static final String COLUMN_AGE = "Age";
private static final String COLUMN_GENDER = "Gender";
private static final String COLUMN_EVENT = "Event";
private static final String COLUMN_TIER = "Tier";

private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + "( " + COLUMN_ID + " INTEGER AUTOINCREMENT, "
+ COLUMN_FIRST_NAME + " VARCHAR(20), " + COLUMN_LAST_NAME + " VARCHAR(20), " + COLUMN_AGE + " INTEGER, " +
COLUMN_GENDER + " VARCHAR(20), " + COLUMN_EVENT + " VARCHAR(20), " + COLUMN_TIER + " INTEGER " + ")";

public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}

public void addAthlete(Athlete athlete){
SQLiteDatabase db = this.getWritableDatabase();

db.execSQL("INSERT INTO " + TABLE_NAME + "(" + COLUMN_ID + ", " + COLUMN_FIRST_NAME + ", " + COLUMN_LAST_NAME
+ ", " + COLUMN_AGE + ", " + COLUMN_GENDER + ", " + COLUMN_EVENT + ", " + COLUMN_TIER + ")" +
" VALUES " + "(" + athlete.getID() + ", '" + athlete.getFirstName()
+ "', '" + athlete.getLastName() + "', " + athlete.getAge() + ", '" + athlete.getGender() + "', '" +
athlete.getEvent() + "', " + athlete.getTier() + ");");
db.close();
}
public Athlete getAthlete(int id){
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + "ID = " + id, null);
if(cursor != null){
cursor.moveToFirst();
}
Athlete athlete = new Athlete();
athlete.setID(Integer.parseInt(cursor.getString(0)));
athlete.setFirstName(cursor.getString(1));
athlete.setLastName(cursor.getString(2));
athlete.setAge(Integer.parseInt(cursor.getString(3)));
athlete.setGender(cursor.getString(4));
athlete.setEvent(cursor.getString(5));
athlete.setTier(Integer.parseInt(cursor.getString(6)));

db.close();
return athlete;
}
public void deleteAllAthletes(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
db.close();
}

public String showTable(){
SQLiteDatabase db = this.getReadableDatabase();
String athletes = "", template = "";
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

template = "\tID\t\t First Name\t\t Last Name\t\t Age\t\t Gender\t\t Event\t\t Tier\n";
if(cursor.moveToFirst()) {
while (cursor.moveToNext()) {

Athlete athlete = new Athlete();
athlete.setID(Integer.parseInt(cursor.getString(0)));
athlete.setFirstName(cursor.getString(1));
athlete.setLastName(cursor.getString(2));
athlete.setAge(Integer.parseInt(cursor.getString(3)));
athlete.setGender(cursor.getString(4));
athlete.setEvent(cursor.getString(5));
athlete.setTier(Integer.parseInt(cursor.getString(6)));

athletes += athlete.getID() + "\t" + athlete.getFirstName() + "\t" + athlete.getLastName()+ "\t" + athlete.getAge()
+ "\t" + athlete.getGender() + "\t" + athlete.getEvent() + "\t" + athlete.getTier();
cursor.moveToNext();
}
}
athletes = template + athletes;
cursor.close();
db.close();
return athletes;
}


}


Interface so the add Athlete fragment can send data to the 2nd fragment:



public interface Communicator {
public void send(Athlete athlete);
}


Thanks for the help


Aucun commentaire:

Enregistrer un commentaire