title: Android - Get _id in database onItemClick
body: <p>So I'm a little bit confused right now. I'm using a simpleCursorAdapter with a inner class which extends BaseAdapter so I can show an ad in my ListView every x items. Now when a user clicks an item in the ListView a dialog pops up and the user can select "Delete" which obviously deletes that row. In my Database Adapter it needs the _id in the database to delete to row so I pass on the ID which I get from the method onItemClick. Now I learned that when using a custom adapter the long id from the onItemClick returns the position so I need to look up the _id in the database. </p>
  
  <p><strong>This is my onItemClick:</strong></p>
  
  <pre><code>myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View viewClicked, 
                  int position, long idInDB) {
  
  Cursor cursor = (Cursor)parent.getItemAtPosition(position);
                  if( cursor != null &amp;&amp; cursor.moveToFirst() ){
                      int rowCol = cursor.getColumnIndex("_id");
                      idInDB1 = cursor.getLong(rowCol);
  </code></pre>
  
  <p>The "idInDB1" is the long that get's passed to my delete method from my Database Adapter.</p>
  
  <p><strong>I'm setting my SimpleCursorAdapter like this:</strong></p>
  
  <pre><code>myList.setAdapter(new ListViewAdapter(getActivity(), myCursorAdapter));
  </code></pre>
  
  <p><strong>Here is the ListViewAdapter class that I have to display ads:</strong></p>
  
  <pre><code>public class ListViewAdapter extends BaseAdapter {
  
  private Activity mContext;  
  private SimpleCursorAdapter listAdapter;  
  private LayoutInflater mLayoutInflater;  
  private int k = 6; // interval ad every 6 items
  int baseItems;
  int noAds; // used for listview offset
  
  // Constructor takes in a BaseAdapter 
  public ListViewAdapter(FragmentActivity activity, SimpleCursorAdapter delegate) {
      mContext = activity;
      listAdapter = delegate; 
      baseItems = listAdapter.getCount();
      noAds  = baseItems / k;
      mLayoutInflater = (LayoutInflater) mContext  
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  }
  
  
  
  @Override
  public int getCount() {
      // Total count includes list items and ads.
      return baseItems + noAds;
  }
  
  @Override
  public Object getItem(int position) {
      // Return null if an item is an ad.  Otherwise return the delegate item.
      if (isItemAnAd(position)) {
          return null;
      }
      return listAdapter.getItem(getOffsetPosition(position));
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  public int getViewTypeCount() {
      return listAdapter.getViewTypeCount() + noAds;
  }
  
  @Override
  public int getItemViewType(int position) {
      if (isItemAnAd(position)) {
          return listAdapter.getViewTypeCount();
      } else {
          return listAdapter.getItemViewType(getOffsetPosition(position));
      }
  }
  
  @Override
  public boolean areAllItemsEnabled() {
      return false;
  }
  
  @Override
  public boolean isEnabled(int position) {
      return (!isItemAnAd(position)) &amp;&amp; listAdapter.isEnabled(getOffsetPosition(position));
  }
  
  private boolean isItemAnAd(int position) {
      if (position &lt; k) return false;
      // Calculate current offset caused by ads already embedded
  
      if (position==k){
          return true;
      }
      else {
          return isItemAnAd(position-k);
      }
  
  }
  
  
  
  
  // Get the position that is offset by the insertion of the ads
  private int getOffsetPosition(int position) {
  
      int currentNoAds = position / k;
      return position - currentNoAds;
  
  }
  
  
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      // Display every n list items
      if (isItemAnAd(position)) {
          if (convertView instanceof AdView) {
              // Don’t instantiate new AdView, reuse old one
              return convertView;
          } else {
              // Create a new AdView
              AdView adView = new AdView(mContext);
              adView.setAdSize(AdSize.SMART_BANNER);
              adView.setAdUnitId("ca-app-pub-2454167205089490/6335584363");
  
              // Disable focus for sub-views of the AdView to avoid problems with
              // trackpad navigation of the list.
              for (int i = 0; i &lt; adView.getChildCount(); i++)
              {
                  adView.getChildAt(i).setFocusable(false);
              }
              adView.setFocusable(false);
  
              // Convert the default layout parameters so that they play nice with
              // ListView.
  
              float density = mContext.getResources().getDisplayMetrics().density;
              int height = Math.round(AdSize.BANNER.getHeight() * density);
              AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                      AbsListView.LayoutParams.MATCH_PARENT,
                      height);
              adView.setLayoutParams(params);
              AdRequest bannerIntermediateReq = new AdRequest.Builder().build();
              adView.loadAd(bannerIntermediateReq);
  
              return adView;
          }
      } else {
  
          // Offload displaying other items to the delegate 
          return listAdapter.getView(getOffsetPosition(position) ,
                  convertView, parent);
  
      }
  }
  }
  </code></pre>
  
  <p>My "idInDB1" is always the _id of the top row in my ListView and I can't seem to figure out why. Anyone?</p>