Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Deleting a row from SQLite table and updating a ListView
    primarykey
    data
    text
    <p>I am new to android development and have come across a problem with deleting a row in a SQLite table that I have not been able to find a solution to. When I try to delete a row nothing is happening. </p> <p>In my activity, called 'MainActivity,' I am using a context menu to call a method to delete an item from the list. This portion of code is as follows:</p> <pre><code>case R.id.delete_program: DBAdapter adap = new DBAdapter(this); adap.open(); long pass = (long) (position + 1); adap.removeFromCurrent(pass); adap.close(); Runnable run = new Runnable() { public void run(){ refreshCurrent(getApplicationContext()); } }; runOnUiThread(run); proAdapt.notifyDataSetChanged(); return true; </code></pre> <p>In 'DBAdapter' the method removeFromCurrent():</p> <pre><code>public boolean removeFromCurrent(long cp_id) { return this.dB.delete(PROGRAMS_TABLE, CP_ID + '=' + cp_id, null) &gt; 0; } </code></pre> <p>In 'MainActivity' the method refreshCurrent():</p> <pre><code>public static void refreshCurrent(Context context) { CURRENT.clear(); DBAdapter adap = new DBAdapter(context); adap.open(); ArrayList&lt;Program&gt; temp = adap.getCurrentPrograms(); for(Program item : temp) { Toast.makeText(context, "Item added: " + item.getName(), Toast.LENGTH_SHORT).show(); CURRENT.add(item); } adap.close(); } </code></pre> <p>I'm using the toast to check if the table has changed but the display has not. From 'DBAdapter' the method getCurrentPrograms():</p> <pre><code>public ArrayList&lt;Program&gt; getCurrentPrograms() { ArrayList&lt;Program&gt; list = new ArrayList&lt;Program&gt;(); Cursor cursor = this.dB.query(PROGRAMS_TABLE, new String[] { CP_ID, CP_NAME, ACTUAL_DAY, D_ID, CP_CYCLE, CP_DAY_ONE }, null, null, null, null, null); int rows = cursor.getCount(); if(cursor != null) cursor.moveToFirst(); for(int i = 1; i &lt;= rows; i++) { list.add(new Program(cursor.getString(cursor.getColumnIndex(CP_NAME)), cursor.getInt(cursor.getColumnIndex(ACTUAL_DAY)), cursor.getInt(cursor.getColumnIndex(CP_DAY_ONE)), cursor.getInt(cursor.getColumnIndex(CP_CYCLE)))); cursor.moveToNext(); } return list; } </code></pre> <p>The ArrayList 'CURRENT' is the list used to populate the ListView. My thinking was that I would be able to delete a row from the table and then repopulate this list as well as the ListView. I was also curious as to what happened to a SQLite table once a row is removed; do the other rows move up a position, or do they stay in the same place?</p> <p>I am still very new to this so any help about my problem or tips for the rest of my code would be much appreciated. Let me know what else I'm doing wrong with my programming.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload