我有一个应用程序,其中有一个RecyclerView
的10项图像和每个TextView
相应的ImageView
。
该应用程序需要具有一个功能,其中有无限数量的“有趣事实”,用户可以为每个项目添加和循环,以及删除显示如下所示的那个。
如果有人能解释如何做到这一点,那将真的有所帮助。
您可以通过onClick事件侦听器上的适配器在所选位置的文本视图中设置edit-view的值。
为此,您需要的是在文本视图中显示的文本数组。
如果是编辑
onclick
任何项目的recyclerView
显示editText
并添加事实按钮。on Click
中:从editText
获取文本并通过以下方式将其设置为数组:arraylist.set(pos,text); adapter.notifyItemChanged(pos);
如果在添加事实按钮的onClick
中添加:从editText
获取文本并通过以下方式将其设置为数组:
arraylist.add(text); adapter.notifyItemInserted(arraylist.size()-1);
@迈克尔·达迪 - 如果有人能解释如何做到这一点,那真的会有所帮助。
我只是解释你的方式来存档这个任务
首先,make POJO or Model class from JSON将是一件容易的事
{
"animalList": [
{
"animalName": "cat",
"animalFactList": [
{
"fact": "this ia fun fact of cat one"
},
{
"fact": "this ia fun fact of cat two"
}
],
},
{
"animalName": "dog",
"animalFactList": [
{
"fact": "this ia fun fact of dog one"
},
{
"fact": "this ia fun fact of dog two"
}
],
},
{
"animalName": "xyz",
"animalFactList": [
{
"fact": "this ia fun fact of xyz one"
},
{
"fact": "this ia fun fact of xyz two"
}
],
}
],
}
这是来自JSON
的模型类
public class Animal {
private List<AnimalListBean> animalList;
public List<AnimalListBean> getAnimalList() {
return animalList;
}
public void setAnimalList(List<AnimalListBean> animalList) {
this.animalList = animalList;
}
public static class AnimalListBean {
/**
* animalName : cat
* animalFactList : [{"fact":"this ia fun fact of cat one"},{"fact":"this ia fun fact of cat two"}]
*/
private String animalName;
private List<AnimalFactListBean> animalFactList;
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
public List<AnimalFactListBean> getAnimalFactList() {
return animalFactList;
}
public void setAnimalFactList(List<AnimalFactListBean> animalFactList) {
this.animalFactList = animalFactList;
}
public static class AnimalFactListBean {
/**
* fact : this ia fun fact of cat one
*/
private String fact;
public String getFact() {
return fact;
}
public void setFact(String fact) {
this.fact = fact;
}
}
}
}
它将临时存储您的数据,当应用程序关闭时,您将丢失所有数据,以便您可以使用任何数据库在本地设备中保存数据。
现在你可以在recyclerview
中使用这个Animal.class设置适配器。