Friday 15 August 2014

android - Getting row id from database by clicking listview after deleting record -


I have created a database with 5 column ID, name, date, month and year, it is being fully prepared , I'm putting it in the list view. The problem now is that when I delete a record from the database I am unable to work on the correct line ID (to delete or edit it). Because the list view does not point to the database, only the wire shows in the list view.

Is there a way that I can get a record to remove even after I work on the right line ID?

>    

public arrestist & lt; String & gt; GetData () {string [] column = new string [] {KEY_ROWID, KEY_NAME, KEY_DATE, KEY_MONTH, KEY_YEAR}; Cursor c = db.query (DATABASE_TABLE, column, empty, empty, empty, empty, empty); String result = ""; Int iRow = c.getColumnIndex (KEY_ROWID); Int iName = c.getColumnIndex (KEY_NAME); Int iDate = c.getColumnIndex (KEY_DATE); Int iMonth = c.getColumnIndex (KEY_MONTH); Int iYear = c.getColumnIndex (KEY_YEAR); ArrayList & LT; String & gt; String_ar = new arreelist & lt; String & gt; (); (C.moveToFirst () ;; c.isAfterLast (); c.moveToNext ()) {Results = c.getString (iRow) + "" + c.getString (iName) + "\ t \ t" + c . GetString (iDate) + "\ t \ t" + c.getString (iMonth) + "\ t \ t" + c.getString (iYear) + "\ n"; String_array.add (results); } Return string_array; }

Call for the string array in the display activity

  ArrayList & lt; String & gt; Data = db.getData (); ArrayAdapter & LT; String & gt; ArrayAdapter = New Arrayheader & lt; String & gt; (This, android.R.layout.simple_list_item_1, data); Lv.setAdapter (arrayAdapter); Lv.setOnItemClickListener (this);   

After getting the line ID, the elements are removed before the database

  public getFROWID for long () {string [] column = New string [] {KEY_ROWID, KEY_NAME, KEY_DATE, KEY_MONTH, KEY_YEAR}; Cursor c = db.query (DATABASE_TABLE, column, empty, empty, empty, empty, empty); C.moveToFirst (); Int iRow = c.getColumnIndex (KEY_ROWID); ROWID = c.getString (iRow); Long value = tall. Parslong (ROIDID); return value; }   

After the record has been deleted, the function in the database class to get the first line ID

  @Override Public Boolean on ITemLongClick (adapterview & lt ;; & Gt; View list view, integer position, long ID) {IMPID = db.getFROWID () + ID; OpenContextMenu (lv); Back true; }   

Update using simple adapters

  from string [] = {BdayDatabase.KEY_NAME}; Int [] to = {R.id.item_name}; List & lt; Hashmap & lt; String, string & gt; & Gt; Listinfo = New Arrestist & lt; Hashmap & lt; String, string & gt; & Gt; (); Listinfo.clear (); Hashmap & lt; String, string & gt; Hm = new Hashmop & lt; String, string & gt; (); Hm.put ("name", BdayDatabase.KEY_NAME); Listinfo.add (hm); Simple Adapter Adapter = New Simple Adapter (this, LISTINFO, R.Ligh Out.it_layout, from, with); Lv.setAdapter (adapter);   

Thank you in advance

Why do not you list view Bind to database instead of creating an array list ???

In this way, you only need to tie the list visit to your primary key as _id and then when you click on a row, The long ID variable is the database that you clicked in the database

No comments:

Post a Comment