1

hope you could help me with this problem. i want to get the rowId of a selected item in a listview from sqlite so that i can pass the value to another activity.

i already fed the listview with info from my sqlite table and use onClickedItem in the listview and use pos + 1 to get the id, but i dont want this solution because whenever i delete an item from the listview i wont be able to get the correct rowId from the database itself... this is my code :

in my DBhelper:

public List<String> getSYAList() {
         List<String> List = new ArrayList<String>();
         try {
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_SYA ;
            Cursor c = ourDatabase.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (c.moveToFirst()) {
                do {
                    List.add((c.getString(0)) + " " +(c.getString(1)) + " "+ (c.getString(2)));

                } while (c.moveToNext());
            }
     } catch (Exception e) {
        Toast.makeText(ourContext, "Error encountered.",
                Toast.LENGTH_LONG).show();
    }
          return List;
    }  

in my activity:

private void loadSYA() { // TODO Auto-generated method stub

    final DBhelper entry = new DBhelper(this);
    entry.open();

    final List<String> sya = entry.getSYAList();
        if(sya.size()>0) // check if list contains items.
        {    

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SYA.this,android.R.layout.simple_dropdown_item_1line, sya);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);



    sqlSYA.setAdapter(arrayAdapter);
    sqlSYA.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


            Intent i = new Intent(SYA.this,SYAInfo.class);
            Bundle b = new Bundle();
            b.putInt("id", (int)position + 1);
            i.putExtras(b);
            startActivity(i);   

            }
        });   }  
             else 
             {
                Toast.makeText(SYA.this,"No items to display",1000).show();
             }  


    entry.close(); 

}

i really hope you anyone could help me to find a solution for this one. thanks in advance guys !!!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You **must** understand that item positions in a ListView **are not** related by any means to the row ids in a table. – Phantômaxx Dec 30 '14 at 08:41
  • @DerGolem yes i do , thats why im really finding it hard to get a solution for this... – user3907236 Dec 31 '14 at 16:56
  • No... nothing hard. In your query, simply add a WHERE clause in which you add the text from the clicked item. OR simply add an hidden field in your custom row with the rowID from the db and pass that to your WHERE clause. Maybe the latter is a bit more complicate. But it offers the best solution, I think. – Phantômaxx Dec 31 '14 at 17:21

0 Answers0