Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:27:37+00:00 2026-06-03T18:27:37+00:00

I have a spinner that show my data list from a database SQLite.. I

  • 0

I have a spinner that show my data list from a database SQLite.. I want to remove a row from Spinner and db when selected then clicked button btn. How can I do this? Thanks

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remove_wf2);

        mDB = new MyDatabase(getApplicationContext()); 
        mDB.open();
        spin = (Spinner)findViewById(R.id.wf_spinner);

        c = mDB.fetchWfs();
        startManagingCursor(c);

        // create an array to specify which fields we want to display
        String[] from = new String[]{WfMetaData.WF_NAME_KEY};
        // create an array of the display item we want to bind our data to
        int[] to = new int[]{android.R.id.text1};
        // create simple cursor adapter
        adapter =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // get reference to our spinner
        spin.setAdapter(adapter);

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                spinnerPos = pos;
                mRowId = id;  // database row id
            }
        });

        //fillSpinner(spin);


        Button btn = (Button)findViewById(R.id.button11);
        btn.setText("Rimuovi");
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDB.delete_byID(mRowId);
                c.requery();


            }
        });
        /*
        Spinner spin = (Spinner)findViewById(R.id.wf_spinner);
        Cursor cur = mDB.fetchWfs();
        startManagingCursor(cur);

        String[] from = new String[] { WfMetaData.WF_NAME_KEY };
        int[] to = new int[] { android.R.id.text1 };
        SimpleCursorAdapter spinAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cur, from, to);
        spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(spinAdapter);

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Cursor c = (Cursor)parent.getItemAtPosition(pos);
                mSpinnerWF = c.getInt(c.getColumnIndexOrThrow(WfMetaData.WF_NAME_KEY));
            }
            @Override
                public void onNothingSelected(AdapterView<?> parent) {
            }
        });*/
        //mDB.close();
    }

My delete method is this:

public boolean delete_byID(long rowId) {
        return mDb.delete(WfMetaData.WF_TABLE, WfMetaData.WF_NAME_KEY + "=" + rowId, null) > 0;

    CharSequence text = "Il Workflow "+ n +" è stato rimosso con successo!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(mContext, text, duration);
    toast.show();
    }

I don’t find any delete method from ID. Is this a db method?

I’ve modified my app like this:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remove_wf2);

        mDB = new MyDatabase(getApplicationContext()); 
        mDB.open();
        spin = (Spinner)findViewById(R.id.wf_spinner);

        c = mDB.fetchWfs();
        startManagingCursor(c);

        // create an array to specify which fields we want to display
        String[] from = new String[]{WfMetaData.WF_NAME_KEY};
        // create an array of the display item we want to bind our data to
        int[] to = new int[]{android.R.id.text1};
        // create simple cursor adapter
        adapter =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // get reference to our spinner
        spin.setAdapter(adapter);

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                spinnerPos = pos;
                mRowId = id;  // database row id
            }
        });

        //fillSpinner(spin);
        mDB.close();

        Button btn = (Button)findViewById(R.id.button11);
        btn.setText("Rimuovi");
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adapter.getItem(spinnerPos);
                mDB.delete_byName((String)adapter.getItem(spinnerPos));
                c.requery();


            }
        });

but I get this error in LogCat(Cast exception):

05-10 15:46:38.586: D/RadioSignalLevel(1758): Gsm Radio Signal level: 4
05-10 15:46:38.586: D/StatusBarPolicy(1758): ERI alert sound is disabled.
05-10 15:46:38.606: D/NetworkService(1939): handle message:800
05-10 15:46:38.606: D/NetworkService(1939): getRawGPRSRegistrationState
05-10 15:46:38.616: I/TransactionService(29090): MMS-STATUS - start transaction service, app version=STABLE4.5.user.4.5.2A-74_OLE-31.2.3.4.Blur_Version.45.31.0.MB860.TIM.en.IT.Thu Sep 22 04:02:02 CST 2011 (log=false) (apn=false) (config=false)
05-10 15:46:38.626: I/SmsReceiverService(29090): MMS-STATUS - start sms receiver service, app version=STABLE4.5.user.4.5.2A-74_OLE-31.2.3.4.Blur_Version.45.31.0.MB860.TIM.en.IT.Thu Sep 22 04:02:02 CST 2011 (log=false) (apn=false) (config=false)
05-10 15:46:38.636: I/TelephonyRegistry(1610): notifyDataConnection: state=0 isDataConnectivityPossible=false reason=nwTypeChanged interfaceName=null networkType=10
05-10 15:46:38.636: I/SYS_MPP(1965): WebtopStatusHandler    updateDataIcon()
05-10 15:46:38.646: D/NetworkService(1939): handle message:800
05-10 15:46:38.646: D/NetworkService(1939): getRawGPRSRegistrationState
05-10 15:46:38.646: I/CBStartup(1939): onServiceStateChanged
05-10 15:46:38.656: I/CBSettings(1939): Reading database: KEY_NAME= db_key_language
05-10 15:46:38.656: I/CBSettings(1939): Reading database: KEY_NAME= db_key_channel


public class MyDatabase {  

    SQLiteDatabase mDb;
    DbHelper mDbHelper;
    Context mContext;
    private static final String DEBUG_TAG = "WFListDatabase";
    private static final String DB_NAME="WFListdb";//nome del db
    private static final int DB_VERSION=1; //numero di versione del nostro db

    public MyDatabase(Context ctx) {
        mContext = ctx;
        mDbHelper = new DbHelper(ctx, DB_NAME, null, DB_VERSION);   //quando istanziamo questa classe, istanziamo anche l'helper (vedi sotto)     
    }

   public void open(){  //il database su cui agiamo è leggibile/scrivibile
            mDb=mDbHelper.getWritableDatabase();

    }

    public void close(){ //chiudiamo il database su cui agiamo
            mDb.close();
    }


    public void insertWf(String name,String cls){ //metodo per inserire i dati
            ContentValues cv=new ContentValues();
            cv.put(WfMetaData.WF_NAME_KEY, name);
            cv.put(WfMetaData.WF_CLASS_KEY, cls);
            mDb.insert(WfMetaData.WF_TABLE, null, cv);
    }



    /*public void removeWf(String name,String cls){ //metodo per inserire i dati
        ContentValues cv=new ContentValues();
        cv.remove(WfMetaData.WF_NAME_KEY);
        cv.remove(WfMetaData.WF_CLASS_KEY);
        mDb.(WfMetaData.WF_TABLE, null, cv);
}*/



    public Cursor fetchAllWfs(){ //metodo per fare la query di tutti i dati
        return mDb.query(WfMetaData.WF_TABLE, new String[]{WfMetaData.WF_NAME_KEY, WfMetaData.WF_CLASS_KEY},null,null,null,null,null);               
    }

    static class WfMetaData {  // i metadati della tabella, accessibili ovunque
    static final String WF_TABLE = "wfs";
    static final String ID = "_id";
    static final String WF_NAME_KEY = "name";
    static final String WF_CLASS_KEY = "class";
    }


    private static final String WF_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  //codice sql di creazione della tabella
                    + WfMetaData.WF_TABLE + " (" 
                    + WfMetaData.ID+ " integer primary key autoincrement, "
                    + WfMetaData.WF_NAME_KEY + " text not null, "
                    + WfMetaData.WF_CLASS_KEY + " text not null);";

    public Cursor fetchWfs(){ //metodo per fare la query di tutti i dati
        return mDb.query(WfMetaData.WF_TABLE, null,null,null,null,null,null);               
}

    public void delete_byName(String n){
     mDb.delete(WfMetaData.WF_TABLE, WfMetaData.WF_NAME_KEY + "='" +n + "'", null);
     //mDb.delete(WfMetaData.WF_TABLE, WfMetaData.WF_NAME_KEY + "=?", new String[] { n });

    CharSequence text = "Il Workflow "+ n +" è stato rimosso con successo!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(mContext, text, duration);
    toast.show();
    }


    public void delete_byID(long rowId) {
        mDb.delete(WfMetaData.WF_TABLE, WfMetaData.WF_NAME_KEY + "=" + rowId, null);

        CharSequence text = "Il Workflow è stato rimosso con successo!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(mContext, text, duration);
        toast.show();
    }

   private class DbHelper extends SQLiteOpenHelper { //classe che ci aiuta nella creazione del db

        public DbHelper(Context context, String name, CursorFactory factory,int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) { //solo quando il db viene creato, creiamo la tabella
            _db.execSQL(WF_TABLE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(DEBUG_TAG, "Upgrading database. Existing contents will be lost. ["
                    + oldVersion + "]->[" + newVersion + "]");
            _db.execSQL("DROP TABLE IF EXISTS " + WF_TABLE_CREATE);
            onCreate(_db);
        }       

    }


    public boolean isEmpty(){
        boolean isEmpty = true;
        Cursor cursor = mDb.query(WfMetaData.WF_TABLE, new String[] { WfMetaData.WF_NAME_KEY }, null, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0)
        {
           isEmpty = false;
        }
        return isEmpty;
    }
}

public class RemoveWorkflow2 extends Activity {

    private EditText nameEditText;
    private EditText classEditText;
    //private EditText idEditText;
    //private int mSpinnerWF;
    Spinner spin;
    WorkflowChoice wf = new WorkflowChoice();

    MyDatabase mDB;
    SimpleCursorAdapter adapter;
    private long mRowId;
    private int spinnerPos;
    private String delString;
    Cursor c;
    //MyDatabase mDB = wf.getDb();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remove_wf2);

        mDB = new MyDatabase(getApplicationContext()); 
        mDB.open();
        spin = (Spinner)findViewById(R.id.wf_spinner);

        c = mDB.fetchWfs();
        startManagingCursor(c);

        // create an array to specify which fields we want to display
        String[] from = new String[]{WfMetaData.WF_NAME_KEY};
        // create an array of the display item we want to bind our data to
        int[] to = new int[]{android.R.id.text1};
        // create simple cursor adapter
        adapter =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // get reference to our spinner
        spin.setAdapter(adapter);

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                Cursor c = (Cursor)(parent.getAdapter().getItem(pos));
                delString = c.getString(c.getColumnIndex(mDB.);
                //spinnerPos = pos;
                //mRowId = id;  // database row id
            }
        });

        //fillSpinner(spin);


        Button btn = (Button)findViewById(R.id.button11);
        btn.setText("Rimuovi");
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDB.delete_byID(mRowId);
                mDB.delete_byName(delString);
                c.requery();


            }
        });

    }

    private void fillSpinner(Spinner s){

        Cursor c = mDB.fetchWfs();
        startManagingCursor(c);

        // create an array to specify which fields we want to display
        String[] from = new String[]{WfMetaData.WF_NAME_KEY};
        // create an array of the display item we want to bind our data to
        int[] to = new int[]{android.R.id.text1};
        // create simple cursor adapter
        SimpleCursorAdapter adapter =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        // get reference to our spinner
        s.setAdapter(adapter);
        }


}

I have a problem in method:

@Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                Cursor c = (Cursor)(parent.getAdapter().getItem(pos));
                delString = c.getString(c.getColumnIndex(mDB.);
                //spinnerPos = pos;
                //mRowId = id;  // database row id
            }

How can I set delString? I don’t have YOUR_COLUMN_ID.. How can I resolve it? THANKS

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:27:41+00:00Added an answer on June 3, 2026 at 6:27 pm

    I’m assuming the button is outside the spinner…

    You don’t really need the position. Set a class variable:

    private int spinnerPos;     
    private long mRowId;
    

    Add this for your spinner:

    spin.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            spinnerPos = pos;
            mRowId = id;  // database row id
        }
    });
    

    Then for your button:

    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
            // Your code to get the string from the adapter
            mDB.delete_byName(yourString);
            c.requery();
    });
    

    By the way, you should leave your DB open until you are exiting the app.

    I see you asking a question about deleting by id. In your database class you would have something like this:

    public boolean delete(long rowId) {
        return mDb.delete(TABLE_NAME, TABLE_ROWID + "=" + rowId, null) > 0;
    }
    

    Hope this helps!

    EDIT

    Better method:

    Private String delString;
    

    Add this for your spinnerListener

    spin.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
                Cursor c = (Cursor) (parent.getAdapter().getItem(pos));
                delString = c.getString(c.getColumnIndex(YourDB.YOUR_COLUMN_ID));       }
    });
    

    Button:

    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            mDB.delete_byName(delString);
            c.requery();
    });
    

    EDIT 2

    First Method for delete (by RowId):

    public boolean delete_byID(long rowId) {
        return mDb.delete(WfMetaData.WF_TABLE, WfMetaData.ID + "=" + rowId, null) > 0;
    }
    

    Second Method for delete (by name):

    public boolean delete_byName(String name) {
        return mDb.delete(WfMetaData.WF_TABLE, WfMetaData.WF_NAME_KEY + "= '" + name + "'", null) > 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that shows loading spinner while fetching data (usually takes
I have a little iPhone app that loads data from a web service. To
I have a long-running report and want to show a wait-spinner to the user
I have an implementation file that load data from rss in the internet, parse
I have within a TabActivity a Spinner that will be generated dynamically. Just to
Hi I have a spinner (spinner5) and whenever the user selects an item that
I have an app that performs some calculations based on data arriving on a
I need to show some data in a grid-like format. For that purpose, I
I have a simple list of strings that I populate into an Adapter per
I have an Android Spinner view in my layout. I would like that spinner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.