ListView onItemClick从数据库获取行? [英] ListView onItemClick get row from Database?

查看:76
本文介绍了ListView onItemClick从数据库获取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android开发还很陌生.我有一个自定义ListView,其中填充了DatabaseTable中的数据. ListView项由CATEGORY,DATE,TITLE和AMOUNT组成.

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.

private class lvIncomeListener implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String item = ???;
        displayItemClicked("List-item Clicked:",item);

    }
}

现在,我想向ListView添加一个OnItemClickListener,以便在单击列表中的项目时,一个DialogFragment将显示该数据库中该特定行中的数据,该数据显示在ListView中,例如

Now, I want to add an OnItemClickListener to the ListView, so that when an item in the list is clicked, a DialogFragment will display the data from that particular row in the Database that is shown in the ListView, like

点击的物品:类别:食物,日期:17/10/25,标题:餐厅,金额:20sek"

"Item Clicked: Category: food, Date: 17/10/25, Title: Restaurant, Amount: 20sek"

...实现这一目标的最佳方法是什么?

... how is the best way to go about to achieve this?

推荐答案

由于您有一个自定义适配器,因此我假设您有一个自定义类,用于存储列表数据,如下所示:

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; }
}

现在,在Activity中的onItemClick方法中,获取像这样的列表数据:

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

这篇关于ListView onItemClick从数据库获取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆