Android onClick将数组值添加到ListView

时间:2016-05-06 21:00:59

标签: android listview

我有一个简单的应用程序,其中包含事实和事实编号。

String facts[] = {"Elephants are the only mammals that can't jump.",
            "Candles will burn longer and drip less if they are placed in the freezer a few hours before using.",
            "Potatoes have more chromosomes than humans.",
            "Butterflies can taste with their hind feet.",
};

String factNumber[] = {
                "Fact 1 of 5000",
                "Fact 2 of 5000",
                "Fact 3 of 5000",
                "Fact 4 of 5000",
};

我有一个喜欢的按钮,当点击按钮时,我希望将事实和事实编号添加到ListView,这样我就可以随时查看我的收藏夹。

我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

创建一个这样的模型类:

class Fact {
    String fact;
    String factNumber;
    boolean favorite;

    //...
}

使用List或这些对象的数组来显示您的数据。在适配器中,您可以检查favorite变量以确定要显示的图标。

OnClickListener中,将此布尔标志更改为相反的值。并更新用户界面。