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

梦想与她 提交于 2019-12-24 10:48:53

问题


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. now when i click the item and pass the position of the clicked item of the listview to the new activity. And try to retrieve the record with that ID it does not return me something or incorrect data is returned. now my question is how can I detect the row id of the clicked item of listview in the database. Now what should be code inside the onItemclick method to retrieve the correct rows from the database.

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) 
        {

        }
    });



}

回答1:


First of all you have to write a query and retrieve id in your database.You must return the id in a cursor.

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:

Cursor cursor = myDB.fetchAllNames();

Then you can get the database id by:

 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. 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.




回答2:


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.

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

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



来源:https://stackoverflow.com/questions/27702638/how-can-i-get-the-row-id-of-the-a-table-in-the-sqlite-database-when-an-item-is-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!