从sqlite数据库获取listView中项的rowId

时间:2014-12-29 12:48:05

标签: android sqlite android-listview rowid

希望你能帮我解决这个问题。 我想从sqlite 获取listview中所选项目的rowId,以便我可以将该值传递给另一个活动。

我已经用我的sqlite表中的信息提供了listview,并在listview中使用onClickedItem并使用pos + 1来获取id,但我不想要这个解决方案因为每当我从listview中删除一个项目时我都无法从数据库本身获取正确的rowId ......这是我的代码:

在我的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;
    }  

在我的活动中:

private void loadSYA(){         // TODO自动生成的方法存根

    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(); 

}

我真的希望你能有人帮我找到解决方案。先谢谢你们!

0 个答案:

没有答案