简体   繁体   English

在列表视图中单击项目时,如何获取sqlite数据库中表的行ID

[英]How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

My code goes here.............! 我的代码在这里.............! when there is a row deleted from the list-view. 当从列表视图中删除一行时。 The indexes of the rows of the listview is changed but the primary key id in the database is not changed. 列表视图的行的索引已更改,但数据库中的主键ID不变。 now when i click the item and pass the position of the clicked item of the listview to the new activity. 现在,当我单击项目并将listview中被单击项目的位置传递给新活动时。 And try to retrieve the record with that ID it does not return me something or incorrect data is returned. 并尝试检索具有该ID的记录,该记录不返回任何内容或返回不正确的数据。 now my question is how can I detect the row id of the clicked item of listview in the database. 现在我的问题是如何在数据库中检测列表视图的单击项的行ID。 Now what should be code inside the onItemclick method to retrieve the correct rows from the database. 现在,onItemclick方法中应包含什么代码,以从数据库中检索正确的行。

Thanks in anticiaption......! 谢谢你……!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DBHelper(this);
    titles = myDb.getAllNames();
    times =  myDb.getAllTimes();

    ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles);
    lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(ad);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {

        }
    });



}

First of all you have to write a query and retrieve id in your database.You must return the id in a cursor. 首先,您必须编写查询并在数据库中检索ID。您必须在游标中返回ID。

Refer the below code snippet: 请参考以下代码段:

public Cursor fetchAllNames() {

        Cursor mCursor = app.myDbHelper.MyDB().query("cardlist", new String[] {"cardid as _id","cardname","carddesc"},
                null, null, null, null, null);
        try{

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }catch(Exception e)
        {
            return mCursor;
        }

    }

Then in your java page: 然后在您的Java页面中:

Cursor cursor = myDB.fetchAllNames();

Then you can get the database id by: 然后,您可以通过以下方式获取数据库ID:

 lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {
      if(cursor!=null){
                    System.out.println("position===>"+position);
                    if(cursor.moveToFirst()){
                        cursor.moveToPosition(position);   
                        String cardid =    cursor.getString(cursor.getColumnIndex("_id"));

                    }
                }
        }
    });

you will get the database id in the string cardid. 您将在字符串cardid中获得数据库ID。 You must use _id if you are using SimpleCursorAdapter else you must replace _id with that of the column name that represents your id in the sqlite database. 如果使用SimpleCursorAdapter,则必须使用_id ,否则必须 _id 替换为表示sqlite数据库中ID的列名。

There's an easy way to achieve so : First, create an object to represent one line in your database. 有一个简单的方法可以做到这一点:首先,创建一个对象来表示数据库中的一行。 Since I don't know what your database contains, let's just call it "Title". 由于我不知道您的数据库包含什么,因此我们仅将其称为“标题”。

public class Title {
    private long id;
    private String title;
    // Constructor, setters and getters
}

Then, create a custom adapter. 然后,创建一个自定义适配器。 It will store a collection of Title which will allow you to get it when an item is clicked. 它会存储一个Title集合,当您单击某个项目时,您可以获取它。

public class TitlesAdapter extends ArrayAdapter<Title> {
    private Context context;
    private List<Title> items;

    public TitlesAdapter(Context context, List<Title> items) {
        super(context, R.layout.row_draweritem, items);

        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { /* ... */ }
}

You can then set your adapter in your activity easily: 然后,您可以轻松地在活动中设置适配器:

this.titlesAdapter = new TitlesAdapter(context, titles);
listView.setAdapter(this.titlesAdapter);

... and define your onItemClickListener so you can get back the row id in your database ...并定义onItemClickListener,以便您可以获取数据库中的行ID

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        long id = titlesAdapter.getItems().get(i).getId();
    }
});

You can learn more about custom adapters in this article : http://www.vogella.com/tutorials/AndroidListView/article.html 您可以在本文中了解有关自定义适配器的更多信息: http : //www.vogella.com/tutorials/AndroidListView/article.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中的ListView项中单击ImageButton时如何刷新sqlite行ID - How to refresh a sqlite row id when an ImageButton is clicked inside a ListView item in Android 单击ListView项时,从数据库获取项的ID - Get id of item from database, when ListView item is clicked 如何获取列表视图中单击的列表项的SQLite列_id值 - How to Get the SQLite column _id value for the list item clicked in listview 单击小部件ListView时如何获取项目数据 - How can i get item data when clicked widget ListView 当单击列表视图项时,如何从sqlite数据库中读取某些数据并将其显示在对话框中? - How do I read certain data from sqlite database and show it in a dialog when a listview item is clicked? 单击recyclerview项目时如何获取sqlite的ID? - How to get id of sqlite when recyclerview item clicked? 单击项目时从列表视图获取ID - Get the id from listview when an item is clicked 单击自定义ListView中的列表项时如何获取数据库的行ID? - How to get row id of database on clicking a list item in a custom ListView? 单击列表视图项从SQLite数据库中获取行ID - Fetching row id from SQLite database on clicking a listview item 如何获取ListView项的ID,而不是ListView上的位置? - How can I get the ID of the ListView Item instead of the position on the listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM