title: DB populated ListView delete selected from DB on Button Click
body: <p>Ok, I'm still kind of new to android and eclipse but the title pretty much says it all. I've got a ListView that I populated from rows in DB. I want to take the selected list view item and delete row from DB when button is clicked and then refresh ListView once this is done. So far my code populates the list, and I'm stuck there. Any help would be greatly appreciated.</p>
  
  <pre><code>public class ViewListingsActivity extends Activity implements OnClickListener {
      SQLiteDatabase db;
      ListView listview;
      Button button;
      SimpleCursorAdapter adapter;
      String value;
      private static final String TAG = DBHelper.class.getSimpleName();
      public static final String DB_ADDRESS = BaseColumns._ID;
      public static final String DB_DESCRIPTION = "Description";
      public static final String DB_URL = "URL";
      final String dbTable = "Realtor_SMS_Table";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.viewlisting);
          listview = (ListView) findViewById(R.id.list);
          button = (Button) findViewById(R.id.button1);
          button.setOnClickListener(this);
          DBHelper dbhelper = new DBHelper(ViewListingsActivity.this);
          db = dbhelper.getWritableDatabase();
  
          Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
          startManagingCursor(cursor);
  
          String[] from = new String[] { DB_ADDRESS, DB_DESCRIPTION, DB_URL };
          int[] to = new int[] { R.id.textlistaddress, R.id.textlistdescription,
                  R.id.textlisturl };
          adapter = new SimpleCursorAdapter(this, R.layout.rowlist, cursor, from,
                  to);
  
          listview.setAdapter(adapter);
          listview.setChoiceMode(1);
  
      }
  
      public void onClick(View arg0) {
          db.delete(dbTable, "_id=" + value, null);
          adapter.notifyDataSetChanged();
  
          Log.d("wtf", "deleted: " + value);
  
      }
  
  }
  </code></pre>
  
  <p>ok, here is my newly edited code:</p>
  
  <pre><code>public class ViewListingsActivity extends Activity implements OnClickListener {
      SQLiteDatabase db;
      ListView listview;
      Button button;
      SimpleCursorAdapter adapter;
      String value;
      OnItemClickListener listener;
      Cursor newcursor;
      String deleteinfo;
  
      private static final String TAG = DBHelper.class.getSimpleName();
      public static final String DB_ADDRESS = BaseColumns._ID;
      public static final String DB_DESCRIPTION = "Description";
      public static final String DB_URL = "URL";
      final String dbTable = "Realtor_SMS_Table";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.viewlisting);
          listview = (ListView) findViewById(R.id.list);
          button = (Button) findViewById(R.id.button1);
          button.setOnClickListener(this);
          DBHelper dbhelper = new DBHelper(ViewListingsActivity.this);
          db = dbhelper.getWritableDatabase();
  
          Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
          startManagingCursor(cursor);
  
          String[] from = new String[] { DB_ADDRESS, DB_DESCRIPTION, DB_URL };
          int[] to = new int[] { R.id.textlistaddress, R.id.textlistdescription,
                  R.id.textlisturl };
          adapter = new SimpleCursorAdapter(this, R.layout.rowlist, cursor, from,
                  to);
  
          listview.setAdapter(adapter);
          listview.setChoiceMode(1);
          listview.setOnItemClickListener(listener);
  
      }
      public void onItemClick (AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
      newcursor = ((SimpleCursorAdapter)listview.getAdapter()).getCursor();
      deleteinfo = newcursor.getString(1);//getString(0);
      }
  
      public void onClick(View arg0) {
          db.delete(dbTable, "_id=" + deleteinfo, null);
          adapter.notifyDataSetChanged();
  
          Log.d("wtf", "deleted: " + deleteinfo);
  
      }
  
  }
  </code></pre>
  
  <p>my deleteinfo String keeps returning null, why is it not retrieving the proper column value?</p>