1

In my databse I have simple table named Place(id,place_name,latitude,longitude,place_details)

I want to delete a row on ListView's item click.

I have tried following:

Place.java

public class Place {
    int _id;
    private String _name;
    private double _latitude,_longitude;
    private String _placedetails;


    public Place(int id, String name, double latitude, double longitude,String placedetails)
        {
            this._id = id;
          this._name = name;
          this._latitude = latitude;
            this._longitude = longitude;
            this._placedetails = placedetails;


    }

    public Place(String name, double latitude, double longitude,String placedetails)
    {

        this._name = name;
        this._latitude = latitude;
        this._longitude = longitude;
        this._placedetails = placedetails;

    }

    public Place() {

    }
    public int getID(){
        return this._id;
    }


    public void setID(int id){
        this._id = id;
    }

    public void setname(String placename)
    {
        this._name = placename;


    }

    public String getname()
    {
        return this._name;


    }

    public void setlatitude(double latitude)
    {
        this._latitude=latitude;


    }

    public double getlatitude()
    {
        return this._latitude;


    }


    public void setlongitude(double longitude)
    {
        this._longitude = longitude;


    }

    public double getlongitude()
    {
        return this._longitude;


    }


    public void  setPlacedetails(String placedetails)
    {

        this._placedetails = placedetails;
    }


    public String getplacedetails()
    {
        return this._placedetails;
    }


}

The Activity in which I want to insert places has something like:

  MySqliteHelper db = new MySqliteHelper(this);

  db.addPlaces(new Place(myplacename, mylatitude, mylongitude, myplacedetails));

As explained here: Android table creation Failure (near "autoincrement": syntax error)? , I don't think I need to explicitly id. It will be created for each object of Place. //Am I wrong here?

In ListView's onitemclicklistener, following code is for deleting place:

  Place place = places_adapter.getItem(pos);
  db.deletePlaces(place);

And In MySQLiteHelper which extends SQLiteHelper, methods are as following:

public void addPlaces(Place place)
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues convalues = new ContentValues();


        convalues.put(KEY_PLACENAME,place.getID());
        convalues.put(KEY_PLACENAME,place.getname());
        convalues.put(KEY_LATITUDE,place.getlatitude());
        convalues.put(KEY_LONGITUDE,place.getlongitude());
        convalues.put(KEY_PLACEDETAILS,place.getplacedetails());

          db.insert(TABLE_NAME, null, convalues);
          Log.d("my","db.insert(TABLE_NAME, null, convalues)");
          Log.d("my", "Values inserted");
          db.close();

    }

    public void deletePlaces(Place place)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        int id = place.getID();

        Log.d("my",Integer.toString(id));
        db.delete(TABLE_NAME, KEY_ID + " = ?",
                new String[]{Integer.toString(id)} );
        db.close();
    }

While deleting, in logcat the value of id is showing always 0....and according table row is not deleteing from database.

Please help me if anyone know anything is wrong.

Community
  • 1
  • 1
xyz
  • 1,325
  • 5
  • 26
  • 50

1 Answers1

1

I solved it.

Every time you insert record in database, don't forget to take its returned value...db.insert returns long value which is nothing rowID.

So I changed

public void addPlaces(Place place){
        db.insert(TABLE_NAME, null, convalues);
    }

to

public long addPlaces(Place place){

long newRowId =  db.insert(TABLE_NAME, null, convalues);
}

and got this rowID in Activity in which I was inserting place:

   Place place =  new Place(myplacename, mylatitude, mylongitude, myplacedetails);
           long tablerowid= db.addPlaces(place);

and also I set rowID here using my setter method:

 place.setID(tablerowid);

Now, i replaced all int ids to long in Place.java and implemented deletePlaces() method as follows:

public void deletePlaces(Place place){
    SQLiteDatabase db = this.getWritableDatabase();

   long id = place.getID();

    db.delete(TABLE_NAME, KEY_ID + " = ?",
            new String[]{Long.toString(id)} );
    db.close();
}
xyz
  • 1,325
  • 5
  • 26
  • 50