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

  • SEARCH
  • Home
  • 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 9049111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:18:45+00:00 2026-06-16T12:18:45+00:00

I have downloaded an example of SQLite from the following link http://www.codegod.com/Android-ListView-with-dynamic-Images-AID590.aspx SQLiteListActivity.java: package

  • 0

I have downloaded an example of SQLite from the following link
http://www.codegod.com/Android-ListView-with-dynamic-Images-AID590.aspx

SQLiteListActivity.java:

package com.codegod.android;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SQLiteListActivity extends ListActivity implements View.OnClickListener {

    private StockAdapter tableRowAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {

            StocksManager stockMgr = new StocksManager(getApplicationContext());

            // clear the table
            stockMgr.clear();

            // Add values to database
            stockMgr.insert("APPL", "Apple Inc.");
            stockMgr.insert("GOOG", "Google Inc.");
            stockMgr.insert("LTSB", "Let's drink some beer");

            // get the values from database
            ArrayList<Stock> stocks = stockMgr.getStocks(); 

            tableRowAdapter = new StockAdapter(
                    getApplicationContext(), R.layout.table_row, stocks);

            setListAdapter(tableRowAdapter);


        } catch (Exception ex) {
            Log.e(ex.toString(), ex.toString());

        }

    }

    @Override
    public void onClick(View v) {
        ImageButton button = (ImageButton) v;
        Stock row = (Stock) button.getTag();

        tableRowAdapter.deleteRow(row);
        tableRowAdapter.notifyDataSetChanged();

    }
}

Stock.java

package com.codegod.android;

public class Stock {
    private String id;
    private String description;

    public Stock(String id, String description) {
        this.id = id;
        this.description = description;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getDescription() {
        return description;
    }

}

StockAdapter.java

package com.codegod.android;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Adapter for Stock-objects 
 *
 */
public class StockAdapter extends BaseAdapter {

    private final List<Stock> rows;

    public StockAdapter(final Context context, final int itemResId,
            final List<Stock> items) {
        this.rows = items;
    }

    public int getCount() {
        return this.rows.size();
    }

    public Object getItem(int position) {
        return this.rows.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    /**
     * Set the content for a row here
     */
    public View getView(int position, View convertView, ViewGroup parent) {

        final Stock row = this.rows.get(position);
        View itemView = null;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.table_row, null);
        } else {
            itemView = convertView;
        }

        // Set the text of the row
        TextView txtId = (TextView) itemView.findViewById(R.id.rowId);
        txtId.setText(row.getId());

        TextView txtDesc = (TextView) itemView.findViewById(R.id.rowDesc);
        txtDesc.setText(row.getDescription());

        // Remember the row for each button so that we can refer to
        // it when the button is clicked
        ImageButton imgButton = (ImageButton) itemView.findViewById(R.id.icon);
        imgButton.setTag(row);

        return itemView;

    }

    /**
     * Delete a row from the list of rows
     * @param row row to be deleted
     */
    public void deleteRow(Stock row) {

        if(this.rows.contains(row)) {
            this.rows.remove(row);
        }
    }
}

StocksManager.java

package com.codegod.android;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.codegod.android.db.OpenDbHelper;

public class StocksManager {

    private OpenDbHelper dbHelper;

    private static final String STOCK_TABLE = "Stock";
    private static final String STOCK_ID = "stock_id";
    private static final String STOCK_DESC = "stock_desc";

    private static final String SELECT_STOCKS = "SELECT * FROM " + STOCK_TABLE;

    public StocksManager(Context context) {
        dbHelper = new OpenDbHelper(context, STOCK_TABLE, STOCK_ID + " TEXT,"
                + STOCK_DESC + " TEXT");
    }

    /**
     * Insert a Stock-value into database
     * @param stockId id of the stock
     * @param stockDesc description of the stock
     * @return success or fail
     */
    public boolean insert(String stockId, String stockDesc) {
        try {

            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();

            ContentValues initialValues = new ContentValues();

            initialValues.put(STOCK_ID, stockId);
            initialValues.put(STOCK_DESC, stockDesc);

            sqlite.insert(STOCK_TABLE, null, initialValues);

        } catch (SQLException sqlerror) {

            Log.v("Insert into table error", sqlerror.getMessage());

            return false;
        }

        return true;

    }

    /**
     * Get all available stocks
     * @return List of stocks
     */
    public ArrayList<Stock> getStocks() {
        ArrayList<Stock> stocks = new ArrayList<Stock>();

        SQLiteDatabase sqliteDB = dbHelper.getReadableDatabase();

        Cursor crsr = sqliteDB.rawQuery(SELECT_STOCKS, null);

        crsr.moveToFirst();

        for (int i = 0; i < crsr.getCount(); i++)
        {
            stocks.add(new Stock(crsr.getString(0), crsr.getString(1)));

            crsr.moveToNext();
        }

        return stocks;
    }

    /**
     * Clear the table
     */
    public void clear() {

        try {

            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();

            sqlite.delete(STOCK_TABLE, "", null);

        } catch (SQLException sqlerror) {

            Log.v("delete from table error", sqlerror.getMessage());

        }   

    }

}

OpenDBHelper.java

package com.codegod.android.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class OpenDbHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;

    private String createStatement = "";

    /**
     * Creates the OpenDbHelper 
     * 
     * @param context app-context
     * @param tableName name of the table to open/create
     * @param fields fields of the table to create
     */
    public OpenDbHelper(Context context, String tableName, String fields) {

        super(context, tableName, null, DATABASE_VERSION);

        this.createStatement  = "CREATE TABLE ";
        this.createStatement += tableName + " (";
        this.createStatement += fields + ");";

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(this.createStatement);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int neVersion) {
        // TODO Auto-generated method stub
    }

}

Layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="4dip">

    <TextView android:id="@+id/rowId" android:layout_width="fill_parent"
        android:layout_height="26dip" android:singleLine="true"
        android:textSize="20dip" android:textColor="#ffffff" android:text="Id..." />

    <TextView android:id="@+id/rowDesc" android:layout_width="fill_parent"
        android:text="Descr..." android:layout_height="20dip"
        android:textSize="12dip" android:layout_below="@+id/rowId"
        android:layout_alignParentBottom="true" android:textColor="#ffffff" />

    <ImageButton android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/delete"
        android:gravity="right" android:layout_alignParentRight="true"
        android:onClick="onClick" />

</RelativeLayout>

which there is a listview connected to the database and also an image button to delete the rows, but I want to change it in order to save the delete results in my database, I mean the changes after deleting won’t be saved now in this project and even when I pull it from the file explorer there are still three rows in my database, how should I change it? Please help me on this issue.

  • 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-16T12:18:46+00:00Added an answer on June 16, 2026 at 12:18 pm

    You need to write database method to remove row from sqlite database. Here is some code snippet

    public void RemoveRow(String id){
        SQLiteDatabase sqliteDB = dbHelper.getReadableDatabase();
        String query = "DELETE FROM Stock WHERE stock_id='"+id+"'";
        sqliteDB.execSQL(query);
    }
    

    And call this method at listview onClick method

    @Override
    public void onClick(View v) {
        StocksManager.RemoveRow();
    }
    

    You will need to initialize StockManager method;

    Edit :

    By doing so, StocksManager.java would be

    package com.codegod.android;
    
    import java.util.ArrayList;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;
    
    import com.codegod.android.db.OpenDbHelper;
    
    public class StocksManager {
    
        private OpenDbHelper dbHelper;
    
        private static final String STOCK_TABLE = "Stock";
        private static final String STOCK_ID = "stock_id";
        private static final String STOCK_DESC = "stock_desc";
    
        private static final String SELECT_STOCKS = "SELECT * FROM " + STOCK_TABLE;
    
        public StocksManager(Context context) {
            dbHelper = new OpenDbHelper(context, STOCK_TABLE, STOCK_ID + " TEXT,"
                    + STOCK_DESC + " TEXT");
        }
    
        /**
         * Insert a Stock-value into database
         * @param stockId id of the stock
         * @param stockDesc description of the stock
         * @return success or fail
         */
        public boolean insert(String stockId, String stockDesc) {
            try {
    
                SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
    
                ContentValues initialValues = new ContentValues();
    
                initialValues.put(STOCK_ID, stockId);
                initialValues.put(STOCK_DESC, stockDesc);
    
                sqlite.insert(STOCK_TABLE, null, initialValues);
    
            } catch (SQLException sqlerror) {
    
                Log.v("Insert into table error", sqlerror.getMessage());
    
                return false;
            }
    
            return true;
    
        }
    
        /**
         * Get all available stocks
         * @return List of stocks
         */
        public ArrayList<Stock> getStocks() {
            ArrayList<Stock> stocks = new ArrayList<Stock>();
    
            SQLiteDatabase sqliteDB = dbHelper.getReadableDatabase();
    
            Cursor crsr = sqliteDB.rawQuery(SELECT_STOCKS, null);
    
            crsr.moveToFirst();
    
            for (int i = 0; i < crsr.getCount(); i++)
            {
                stocks.add(new Stock(crsr.getString(0), crsr.getString(1)));
    
                crsr.moveToNext();
            }
    
            return stocks;
        }
    
        /**
         * Remove selected stock
         * @return Void
         */
        public void RemoveRow(String id){
            SQLiteDatabase sqliteDB = dbHelper.getReadableDatabase();
            String query = "DELETE FROM Stock WHERE stock_id='"+id+"'";
            sqliteDB.execSQL(query);
        }
    
        /**
         * Clear the table
         */
        public void clear() {
    
            try {
    
                SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
    
                sqlite.delete(STOCK_TABLE, "", null);
    
            } catch (SQLException sqlerror) {
    
                Log.v("delete from table error", sqlerror.getMessage());
    
            }   
    
        }
    
    }
    

    And SQLiteListActivity.java:

    package com.codegod.android;
    
    import java.util.ArrayList;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ImageButton;
    import android.widget.ListView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class SQLiteListActivity extends ListActivity implements View.OnClickListener {
    
        private StockAdapter tableRowAdapter;
        private StocksManager stockMgr;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            try {
    
                //Initialize StockManager Database class
                stockMgr = new StocksManager(getApplicationContext());
    
                // clear the table
                stockMgr.clear();
    
                // Add values to database
                stockMgr.insert("APPL", "Apple Inc.");
                stockMgr.insert("GOOG", "Google Inc.");
                stockMgr.insert("LTSB", "Let's drink some beer");
    
                // get the values from database
                ArrayList<Stock> stocks = stockMgr.getStocks(); 
    
                tableRowAdapter = new StockAdapter(
                        getApplicationContext(), R.layout.table_row, stocks);
    
                setListAdapter(tableRowAdapter);
    
    
            } catch (Exception ex) {
                Log.e(ex.toString(), ex.toString());
    
            }
    
        }
    
        @Override
        public void onClick(View v) {
            ImageButton button = (ImageButton) v;
            Stock row = (Stock) button.getTag();
    
            tableRowAdapter.deleteRow(row);
            tableRowAdapter.notifyDataSetChanged();
    
            //Call DB Method to remove stock from database
            stockMgr.RemoveRow(row.getId());
        }
    }
    

    I haven’t got a chance to test it out but I’m sure it will work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded the CrossFilter example from the following link: https://github.com/square/crossfilter/tree/gh-pages and it seems
I have downloaded the example of Adobe for using Starling at: http://www.whacksite.com/ from GitHub.
I have downloaded the example code/demo from: http://www.technowise.in/2009/12/uploadify-aspnet-c-vb-examples-demo.html and when I try to run
I have downloaded example session bean servlet from here http://www.roseindia.net/jboss/sessionbeanservlet.shtml , download is under
I have downloaded Java API documentation from http://www.oracle.com/technetwork/java/javase/downloads/index.html#docs and have supposedly attached it to
I have downloaded the two releases of python 2.6.8 from here http://www.python.org/getit/releases/2.6.8/ and I
all I have downloaded source code from google android website following google's guide. My
I have downloaded the jquery scroll script (http://www.position-absolute.com/articles/better-html-anchor-a-jquery-script-to-slide-the-scrollbar/) which will scroll to your anchor
I have downloaded facebook-java-api-3.0.2-bin from http://code.google.com/p/facebook-java-api/ for connecting to face book.I want to use
I have 2 SQLite databases, one downloaded from a server ( server.db ), and

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.