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 9286487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:25:44+00:00 2026-06-18T19:25:44+00:00

may i ask for help…i manage to add data to database..but i got problem

  • 0

may i ask for help…i manage to add data to database..but i got problem to delete the data..because i can’t delete based on ID from my ListView…how to get the ID??..this is my code..

This class name as ViewActivity.java where i display my database content…i only display one column only in my ListView

   String query = "select * from " + helper.TABLE_PEKERJA;

        Cursor c = database.rawQuery(query, null);

        if(c!=null)
        {
            c.moveToFirst();
            int getNamaIndex = c.getColumnIndex(helper.COL_NAMA);

            if(c.isFirst())
            {
                do{
                    String nama = c.getString(getNamaIndex);
                    result.add(nama);
                }while(c.moveToNext());
            }
        }

        this.setListAdapter(new ArrayAdapter(this,  android.R.layout.simple_list_item_1,result));
        getListView().setTextFilterEnabled(true);

in my DBHelper, i create a method for delete row from my database table which is here..

public void deleteData(SQLiteDatabase db, String id)
{
    db.delete(TABLE_PEKERJA, COL_ID + " = " +id, null);
}

then still in the class ViewActivity.java, i implement method onListItemClick

    @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();        

    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
    helper.deleteData(database, keyword);
    Toast.makeText(this, "Data " + keyword + " removed" , Toast.LENGTH_SHORT).show();

}

also got this error..this error only i can view…this error at part

Object o = this.getListAdapter().getItem(position);

02-09 02:34:23.729: E/Database(28923):  at com.example.untukmuna.ViewContentActivity.onListItemClick(ViewContentActivity.java:69)

So, my question is

1) How do i delete my database table row, when i press item in ListView based from ID
2) when creating database, there has db.open()…but where should i close database buy putting db.close() …??

thanksss~~ hope you all understand my question.. ^_^

  • 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-18T19:25:45+00:00Added an answer on June 18, 2026 at 7:25 pm

    the way that it is conventionally done is that you simply get the id from the list click parameter.

    protected void onListItemClick(ListView l, View v, int position, long id) <------
    

    But you can’t immediately do this because your ListView is hooked up to an ArrayAdapter and not a CursorAdapter. Right now, that id will either always be 0 or some non-sense value. I would recommend that you change your adapters if you need to do tasks like this.

    But if you need your adapters as is, then what you could do is make a corresponding id collection of some sort that you fill as you loop through your database to fill result. Then it would be as simple as getting the position from the onListItemClick parameter then getting the id from the id collection.

    you’d of course, have to make sure that you’re deleting the value off this collection as you’re deleting the row of the listview.


    try the following:

    private DBHelper helper;
    private SQLiteDatabase db;
    private ArrayList<String> result = new ArrayList<String>();
    private ArrayList<Long> idList = new ArrayList<Long>();
    private ArrayAdapter<String> adapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        helper = new DBHelper(this);
        db = helper.getWritableDatabase();      
    
        String query = "select * from " + helper.TABLE_PEKERJA;
        Cursor c = db.rawQuery(query, null);
        int getIdIndex = c.getColumnIndex(helper.COL_ID);
        int getNamaIndex = c.getColumnIndex(helper.COL_NAMA);
    
        while (c.moveToNext()) {
            idList.add(c.getLong(getIdIndex));
            result.add(c.getString(getNamaIndex));
        }
    
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result);
        this.setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }
    
    public boolean deleteData(Long id) {
        return db.delete(TABLE_PEKERJA, COL_ID + " = " + id, null) > 0;
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Long idValue = idList.get(position);
        boolean isDeleted = deleteData(idValue);
        Toast.makeText(this, "boolean value for database deletion: " + String.valueOf(isDeleted), Toast.LENGTH_SHORT).show();
    
        idList.remove(position);
        result.remove(position);
        adapter.notifyDataSetChanged(); //refresh so that we can see listview changes
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like to ask something that may be simple and stupid, but I can't
I may not be the first person to ask this question but I can't
Why you may ask? Because i have built the app on mysql and need
May i ask you how can i calculate the period by deduct two time
This may be a stupid question to ask, but still I need to know
This may be rather noobish but I'm gonna ask anyhow. I have a class
This may not be the kind of question one should ask on StackOverflow, but
This may sound like a bit of a rhetorical question, but I ask it
This may not be a programming question, but I don't know where to ask
This may be a stupid question, but I have to ask! I have the

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.