ListView onItemClick get row from Database?

后端 未结 1 1402
无人及你
无人及你 2021-01-24 07:18

Im fairly new to Android development. I have a custom ListView that is populated with data from a DatabaseTable. The ListView-items consist of CATEGORY, DATE, TITLE and AMOUNT.

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-24 08:14

    Since you have a custom adapter, I assume you have a custom class where you store your list data maybe like this:

    public class YourListData {
    
        private String catagory;
        private String date;
    
        public YourListData(){
        }
    
    
        public void setCatagory(String cat){ this.catagory = cat; }
        public void setDate(String date){ this.date = date; }
    
        public String getCatagory(){ return this.catagory; }
        public String getDate() { return this.date; }
    }
    

    Now in your onItemClick method in your Activity get the list data like this:

    mYourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            YourListData data = (YourListData) parent.getItemAtPosition(position);
            String cat = data.getCatagory();
            String date = data.getDate();
            //...
    
            //Send data to your fragment
        }
    });
    

    0 讨论(0)
提交回复
热议问题