title: Gathering SQLite row id to delete entry from context menu
body: <p>I query my database and it returns sorted arrays to populate a <code>ListView</code>. When I click on the <code>ListView</code> I can obtain the Row ID according to my database, but my context menu can't access the same array, any guidance on how to go about fixing this? Most of the examples I found online won't help because my database's row ID does not match the <code>ListView</code> id because of the sorting that goes on.</p>
  
  <p>This calls the database and displays the info:</p>
  
  <pre><code>public class SQLView extends ListActivity {
      private SimpleAdapter adapter;
  ListView drinkList;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
  
      super.onCreate(savedInstanceState);
      // Full Screen
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.sqlview);
  
      // Definitions
      SharedPreferences someBar;
      someBar = getSharedPreferences("BarPreferences", 0);
      String originalBar = someBar.getString("currentBar", "My Local Bar");
      originalBar = originalBar.replace("'", "''");
  
      // Open the database and retrieve the drinkNames and the ABV values
      DBmanager info = new DBmanager(this);
      info.open();
      customObject data = info.sortData(originalBar);
      String[] drinkNames = data.getNames();
      String[] drinkScores = data.getScores();
      final String[] drinkRowId = data.getRowId();    
      info.close();
  
      drinkList = (ListView) getListView();
  
      // Create the mapping
      String[] from = new String[] { "rowid", "col_1" };
      int[] to = new int[] { R.id.item_tvdrinkName, R.id.item_tvAbvValue };
  
          List&lt;HashMap&lt;String, String&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      for (int i = 0; i &lt; drinkNames.length; i++) {
          HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
          map.put("rowid", "" + drinkNames[i]);
          map.put("col_1", "" + drinkScores[i]);
          fillMaps.add(map);
  
      }
  
      adapter = new SimpleAdapter(this, fillMaps, R.layout.list_item, from,
              to);
      drinkList.setAdapter(adapter);
  
      // Register the ListView for Context menu
      registerForContextMenu(getListView());
      drinkList.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; a, View v, int position,
                  long id) {
  
              Toast.makeText(SQLView.this,
                      "Row ID of database entry " + drinkRowId[(int) id], //This returns the SQLite Row ID
                      Toast.LENGTH_SHORT).show();
          }
      });
  
  }
  
  public void onCreateContextMenu(ContextMenu menu, View v,
          ContextMenuInfo menuInfo) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.context_menu, menu);
  }
  
  public boolean onContextItemSelected(MenuItem item) {
      // ******THIS IS THE CONTEXT MENU IN QUESTION
  
      AdapterContextMenuInfo information = (AdapterContextMenuInfo) item.getMenuInfo();
      long index = information.id;
      switch (item.getItemId()) {
      case R.id.edit:
          Toast.makeText(SQLView.this, "Edit " + index, Toast.LENGTH_SHORT).show(); //This returns the Listview index
          break;
      case R.id.delete:
          Toast.makeText(SQLView.this, "Delete "+ index, Toast.LENGTH_SHORT).show();
          break;
  
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my database helper class (<code>sortData()</code> is the method of interest, only so you can see it return arrays):</p>
  
  <pre><code>public class DBmanager {
  
  public static final String KEY_ROWID = "_id";
  public static final String KEY_NAME = "drink_name";
  public static final String KEY_BAR = "bar_name";
  public static final String KEY_SCORE = "drink_score";
  public static final String KEY_VOL = "volume";
  public static final String KEY_PRICE = "price";
  public static final String KEY_DRINKABV = "abv";
  
  // Strings for ABV
  
  public static final String KEY_ABV_ID = "_id";
  public static final String KEY_NAME_ABV = "drink_name";
  public static final String KEY_ABV = "abv_value";
  
  private static final String DATABASE_NAME = "DBmanagerdb";
  private static final String DATABASE_TABLE = "drinkTable";
  private static final String ABV_TABLE = "abvTable";
  
  private static final int DATABASE_VERSION = 1;
  
  private DbHelper ourHelper;
  private final Context ourContext;
  private SQLiteDatabase ourDatabase;
  
  private static class DbHelper extends SQLiteOpenHelper {
  
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
  
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                  + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_BAR
                  + " TEXT NOT NULL, " + KEY_NAME + " TEXT NOT NULL, "
                  + KEY_PRICE + " DOUBLE, "+ KEY_VOL + " DOUBLE, "
                  + KEY_DRINKABV + " DOUBLE, " + KEY_SCORE + " DOUBLE);");
  
          db.execSQL("CREATE TABLE " + ABV_TABLE + " (" + KEY_ABV_ID
                  + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME_ABV
                  + " TEXT NOT NULL, " + KEY_ABV + " DOUBLE);");
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + ABV_TABLE);
          onCreate(db);
      }
  }
  
  public DBmanager(Context c) {
      ourContext = c;
  }
  
  public DBmanager open() throws SQLException {
      ourHelper = new DbHelper(ourContext);
      ourDatabase = ourHelper.getWritableDatabase();
      return this;
  }
  
  public boolean deleteDatabase(Context context) {
      return context.deleteDatabase(DATABASE_NAME);
  }
  
  public void close() {
      ourHelper.close();
  }
  
  public long createEntry(String bar, String name,Double price, Double vol, Double abv, Double score) {
      // TODO Auto-generated method stub
      ContentValues cv = new ContentValues();
      cv.put(KEY_BAR, bar);
      cv.put(KEY_NAME, name);
      cv.put(KEY_PRICE, price);
      cv.put(KEY_VOL, vol);
      cv.put(KEY_DRINKABV, abv);
      cv.put(KEY_SCORE, score);
      return ourDatabase.insert(DATABASE_TABLE, null, cv);
  }
  
  //***************************THIS IS THE METHOD OF INTEREST
  public customObject sortData(String bar) {
      // TODO Auto-generated method stub
      String[] columns = new String[] { KEY_ROWID, KEY_BAR, KEY_NAME,
              KEY_SCORE };
  
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BAR + " ='"
              + bar + "'", null, null, null, KEY_SCORE + " DESC");
  
      int iName = c.getColumnIndex(KEY_NAME);
      int iscore = c.getColumnIndex(KEY_SCORE);
      int iRow = c.getColumnIndex(KEY_ROWID);
  
      c.moveToLast();
      String[] names = new String[c.getCount()];
      double[] scores = new double[c.getCount()];
      String[] scores1= new String[c.getCount()];
      String[] rowid = new String[c.getCount()];
  
      String result = "";
      String myScores = "";
      String rows="";
  
  
      int count = 0;
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
          rowid[count] = c.getString(iRow);
          scores[count] = c.getDouble(iscore);
          DecimalFormat df = new DecimalFormat("#.##");
  
          scores1[count]= Double.toString(Double.valueOf(df.format(scores[count]))); 
  
          /* in case i break something later 
          for (int i =0; i&lt;scores.length;i++){
  
              scores1[i]= Double.toString(Double.valueOf(df.format(scores[i]))); 
              System.out.println(scores1[i]);
          }
           */
  
          //scores1[count] = Double.toString(c.getDouble(iscore));
  
  
  
          names[count] = c.getString(iName);
          //Below is for string formatting
          //result = result + names[count] + "\n";
          //myScores = myScores + String.format("%.2f", scores[count]) + "\n";
          count++;
      }
      c.close();
      //Used to format the array so it's pretty pretty
  
      return new customObject(names, scores1, rowid);
  }
  
  public class customObject {     
        private String[] names; //array2
        private String[] scores; //array1
        private String[] rowid;
  
        public customObject(String[] names, String[] scores, String[] id){
           this.names = names;
           this.scores = scores;
           this.rowid = id;
        }
  
        public String[] getNames() {
            return names;
        }
  
        public String[] getScores() {
            return scores;
        }
  
        public String[]getRowId() {
                return rowid;
        }
  
        //Setters + getters. Etc.
  }
  
  
  public String getName(long l) throws SQLException {
      // TODO Auto-generated method stub
      String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_SCORE };
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
              + l, null, null, null, null);
      if (c != null) {
          c.moveToFirst();
          String name = c.getString(1);
          return name;
      }
      return null;
  }
  
  public String getscore(long l) throws SQLException {
      // TODO Auto-generated method stub
      String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_SCORE };
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
              + l, null, null, null, null);
      if (c != null) {
          c.moveToFirst();
          String score = c.getString(2);
          return score;
      }
      return null;
  }
  
  public void updateEntry(long lRow, String mName, String mscore)
          throws SQLException {
      // TODO Auto-generated method stub
      ContentValues cvUpdate = new ContentValues();
      cvUpdate.put(KEY_NAME, mName);
      cvUpdate.put(KEY_SCORE, mscore);
      ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow,
              null);
  }
  
  public void deleteEntry(long lRow1) throws SQLException {
      // TODO Auto-generated method stub
      ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);
  }
  
  public boolean deleteEverything(Context context) {
      // TODO Auto-generated method stub
      return context.deleteDatabase(DATABASE_NAME);
  }
  
  public String[] viewABV() {
      // TODO Auto-generated method stub
      String[] columns = new String[] { KEY_ABV_ID, KEY_NAME_ABV, KEY_ABV};
  
      Cursor c = ourDatabase.query(ABV_TABLE, columns, null, null, null, null, null);
  
  
      int iDrink = c.getColumnIndex(KEY_NAME_ABV);
      int iABV = c.getColumnIndex(KEY_ABV);
  
  
      int precounter = 0;
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
          precounter++;
      }
  
      String[] drinks;
      double[] abv;
      int count = 0;
      // figure out how to preallocate better
  
      drinks = new String[precounter];
      abv = new double[precounter];
  
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
          abv[count] = c.getDouble(iABV);
          drinks[count] = c.getString(iDrink);
          count++;
      }
      c.close();
      //Real similar to the other ones, we just don't format it as a long string.
      return drinks;
  }
  
  public long createEntryABV(String name, String abv) {
      // TODO Auto-generated method stub
      ContentValues cv = new ContentValues();
      double value = Double.parseDouble(abv);
      cv.put(KEY_NAME_ABV, name);
      cv.put(KEY_ABV, value);
      return ourDatabase.insert(ABV_TABLE, null, cv);
  }
  
  
  public String getabv(String l) throws SQLException {
  
      Cursor c = ourDatabase.rawQuery("SELECT * FROM abvTable WHERE TRIM(drink_name) = '"+l.trim()+"'", null); 
  
      if (c != null) {
          c.moveToFirst();
          String abv = c.getString(c.getColumnIndex("abv_value"));
          return abv;
      }
      return null;
  }
  
  
  }
  </code></pre>