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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:30:28+00:00 2026-06-16T16:30:28+00:00

I created database and Y display record on listview,if i long pressed on listview

  • 0

I created database and Y display record on listview,if i long pressed on listview record then context menu is open. In context menu, there are two operation first,Show operation and second delete operation. If i select show then full record is show in next activity.

ListActivity.java

public class ListActivity extends Activity {
    ListView lv;
    ArrayList<String> items = new ArrayList<String>();
    ArrayList<String> items_id = new ArrayList<String>();
    dbhelper dh;
    SQLiteDatabase db;
    int index_id;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Button btn;
        btn = (Button)findViewById(R.id.newProjectlist);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub


                    Intent intent= new Intent(ListActivity.this,NewProject.class);
                startActivity(intent);
            }
        });  
        lv = (ListView)findViewById(R.id.projectListView);
        setUpList();
        dh = new dbhelper(this);
        Cursor c = getAllData();
        showAllData(c);  
        registerForContextMenu(lv);      
    }

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

        if(v.getId() == R.id.projectList){
            menu.setHeaderIcon(R.drawable.ic_launcher);
            menu.setHeaderTitle("Record List");
            menu.add(0,1,menu.NONE,"Delete Record");
            menu.add(0,2,menu.NONE,"Show Record");
        }
    }

    @Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
        int itemId = item.getItemId();
        if(itemId == 1)
        {
            AdapterView.AdapterContextMenuInfo menuinfo;
            menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
            index_id = menuinfo.position;
            deleteUserData(index_id);
        }
        if(itemId == 2){
            AdapterView.AdapterContextMenuInfo menuinfo;
            menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
            index_id = menuinfo.position;
            editUserData(index_id);
        }
    return super.onContextItemSelected(item);
}

    private void editUserData(int indexid) {
        // TODO Auto-generated method stub
        String user_id = items_id.get(indexid);
        Intent intEdit = new Intent(ListActivity.this, MainActivity.class);
        intEdit.putExtra("EditID", user_id);
        startActivityForResult(intEdit, 1);
    }

    private void deleteUserData(int indexid) {
        // TODO Auto-generated method stub
        String user_id = items_id.get(indexid);
        //Toast.makeText(getApplicationContext(), "id " + user_id, 1).show();
        db = dh.getWritableDatabase();
        db.delete("timer","_ID" + "=" + user_id, null);
        finish();
//      Toast.makeText(getApplicationContext(), "Record deleted successfully", 1).show();
    }

    private void showAllData(Cursor c) {
        // TODO Auto-generated method stub
        while(c.moveToNext())
        {
            int id = c.getInt(0);
            String name = c.getString(1);
            String timerpoint1 = c.getString(2);
            String timerpoint2 = c.getString(3);
            String timerpoint3 = c.getString(4);
            String servicetime1 = c.getString(5);
            String servicetime2 = c.getString(6);
            items.add("" + name);
            items_id.add(""+id);
        }
        setUpList();
    }

    private Cursor getAllData() {
        // TODO Auto-generated method stub
        db = dh.getReadableDatabase();
        //String sortorder = dh.Name + "DESC";
    //  String query ="SELECT DISTINCT name FROM timer";
    //  Cursor c = db.rawQuery(query, null);
    //   if (c != null) {
    //            c.moveToFirst();
    //        }
        String[] cols = new String[] {dh.Name, "name"};
        Cursor c = db.query(true, "timer",null,null,null,null,"ASC",null, null);   
        //Cursor cur = db.query(Distinct,"timer", null, null, null, null, null, null, null);
        startManagingCursor(c);
        return c;   
        }
    private void setUpList() {
        // TODO Auto-generated method stub
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
    }   

    }

If I select delete then delete the record. Using Context menu we can achive this goal. I have some code for that application but the code is not working properly. So, please help for this.

  • 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-16T16:30:30+00:00Added an answer on June 16, 2026 at 4:30 pm

    Please provide more details on what you are seeing, however looking at your code i’m guessing that the listview is probably staying in the same state, and not refreshing after you delete. If that is the case you probably need to re-query your cursor. This method is deprecated, however you should be able to use it to validate if that is the problem, or just grab a new instance of the cursor and load it as the docs suggest.

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

Sidebar

Related Questions

My TextSwitcher for each record in ListView should display first value ( text1 )
I successfully created database on my local server using Code First, migrated it to
I need to get the record from database and i want to display that
I have some records. Upon clicking on every record there information needs to display
I was given a task to display when a record in the database was
I am attempting to create a Stored Procedure for a newly created database. However
I created a database on my computer, with MySQL 5.5 and MySQL Workbench 5.2.34
I created a database and created some tables on it. But in some point
iv created a database with some tables and populated them using SQL server 2008,
I created a database for a site i'm doing using Django as the admin

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.