hi im very new to android so i have this function that needed to add and retrieve from my database. so far here is what i have, so i have this set of java files newLoc.java contains the code that responds when the save button is clicked by the user. Area.java contains the getter and setter. DBHandler.java have the database stuff. In the add area i have logged it so that i can check if it is added or not. the problem is when im reading the table. in my newLoc.java, im also calling the getALLAREA function to check wheter the record was inserted but the only ouput im getting is the ID of the records in the database any idea wht i am doing wrong? the code are here
newLoc.java
here are the codes responding to when the user clicked the save button. it gets the data from the editText then calls the DBHanlder to add the data
public void onClick(View v)
{
String text ="user created";
Toast tempMessage =
Toast.makeText(newLoc.this,
text,
Toast.LENGTH_SHORT);
tempMessage.show();
areaname = narea.getText().toString();
DBHandler db = new DBHandler(newLoc.this);
Log.d("Insert: ", "Inserting .." + areaname);
db.addNArea( new Area(areaname));
Log.d("Reading: " , "Reading users..");
List<Area> area = db.getALLAREA();
for(Area ar : area)
{
String arean = ar.getArea();
Log.d("Areas" , arean);
}
}
Area.java
this is my getter and setter from the area
public class Area
{
private int arID;
private String narea;
public Area()
{
}
public Area(String area)
{
this.narea = area;
}
public void setArea(String area)
{
this.narea = area;
}
public String getArea()
{
return narea;
}
}
DBHandeler.java
this is where the database stuff is done. in the addNArea, i have put a log inside the try to check if the data was added. the log is printing the data from the user so i think it is properly inserting because i wasn't able to see the error from the log
public void addNArea(Area are)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ANAME, are.getArea());
try
{
db.insert(TABLE_AREA, null, values);
Log.d("Insert: ", "Inserting .." + are.getArea());
}
catch (SQLException e)
{
Log.d("error: " , "" + e);
}
db.close();
}
//view areas
public List<Area> getALLAREA()
{
List<Area> arealist = new ArrayList<Area>();
String selectQuery = "SELECT * FROM " + TABLE_AREA;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Area allarea = new Area();
allarea.setArea(cursor.getString(0));
arealist.add(allarea);
} while (cursor.moveToNext());
}
return arealist;
}
so the problem now is that in my newLoc.java i can't see the list of the areas i have added. any ideas on what i am doing wrong? thanks so much in advance!