开发者

Getting What Was Longclick In Listview

For some reason my database is failing to execute a .delete method.

I have this check:

if(db.delete(clicked)){
            Log.i("MainAct.class","Deleted row: " + clicked + " successfully");
        } else {
            Log.i("MainAct.class","FAILURE Deleting row: " + clicked + " UNsuccessfully");
        }

It keeps returning "FAILURE" so I figure that means it is not deleting

public boolean delete(String row){
    open();
    Log.i("SmsDatabase.java", "DELETING: " + TABLE_NAME +
            "    id =?    " + "id = '" + row + "'");
     return this.db.delete(TABLE_NAME, "id = '" + row + "'", null) > 0;
}

It is called fine (btw, the open() method has a if(!db.isOpen()) so it doesn't just always open my database) but it returns false 开发者_Python百科for some reason.

I tried this:

return this.db.delete(TABLE_NAME, "id =?", new String[]{row}) > 0;

But in the Log it returns weird characters along the line of: "[Lstring@406865" (dont remember exactly) for String clicked. And obviously that is not what I want.

Why is it doing this?


I'm going to make some assumptions in my answer. (1) The list of whatever you are wanting the user to be able to select for removal is filled with data that was obtained from the database that you are removing it from, (2) You have done the setup in onCreate or something like that to setup the listView's click listener, or (3) set up the listview to be registered for a Context menu. I suspect that you are just not getting the correct ID for the item to be deleted.

In my setup I have a listview that has registered for a context menu, I use the context menu to let them select to edit or remove the item from the list so in my onCreate I call registerForContextMenu(this.listView); In my context menu I have:

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
   {
      menu.setHeaderTitle("Update Item");
      menu.add(Menu.NONE, MENU_EDIT_ITEM,   0, "Edit Item");
      menu.add(Menu.NONE, MENU_REMOVE_ITEM, 1, "Remove Item");
   }

Then

   @Override
   public boolean onContextItemSelected(MenuItem item)
   {
      return (applyMenuChoice(item) || super.onContextItemSelected(item));
   }

Then

   private boolean applyMenuChoice(MenuItem item)
   {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

      switch (item.getItemId())
      {
         case MENU_EDIT_ITEM:
         {
            // Since the user wants to edit an existing device we need to pass the
            // database id of the device - not it's position - to the details screen
            long idNum = info.id;
            Intent i = new Intent(getApplicationContext(), DeviceDataActivity.class);
            i.putExtra(DeviceListViewActivity.ID_EXTRA, String.valueOf(idNum));
            startActivityForResult(i, 0);
            break;
         }
         case MENU_REMOVE_ITEM:
         {
            long idNum = info.id;
            deviceDatabaseHelper.deleteItem(String.valueOf(idNum));
            initList();
            break;
         }
         default:
         {
            return false;
         }
      }
      return true;

   }// end applyMenuChoice

In my database helper's code I call the following:

   public void deleteItem(String id)
   {
      SQLiteDatabase db = getWritableDatabase();
      db.delete(TABLE_NAME, _ID + "=?", new String[]
      { id });
      db.close();
   }

Another helpful function to get a particular item. Note if remember to replace Item/YOUR_ITEM with your object and update the query with your table name (YOUR_TABLE) and the BaseColumns._ID as needed.

   /**
    * Use this method to return a single item object based on it's id
    * NOTE - from calling classes id may not be equal to position
    * @param id - the ID of the device being requested
    * @param db - the database to get it from
    * @return YOUR_ITEM - that device associated with the id
    */
   public static YOUR_ITEM getById(String id, SQLiteDatabase db)
   {
      String[] args = { id };
      Cursor c = db.rawQuery( "SELECT * FROM YOUR_TABLE WHERE " + 
                              BaseColumns._ID + "=?", args );
      c.moveToFirst();
      Item result = new Item().loadFrom(c);
      c.close();
      return (result);
   }

I'm not a very experienced DB guy, so I relied heavily on a lot of the examples out that that had coded a database helper class (extends SQLiteOpenHelper) that facilitated getting and putting data into the SQLiteDataBase. Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜