简体   繁体   中英

Saving the values of EditText in a RecyclerView

I have an app where there is a RecyclerView of 10 items with images and a corresponding TextView for each ImageView .

The app needs to have a functionality where there is an unlimited number of "fun facts" the user can add and cycle through for each item, as well as delete the one displayed as shown below.

If anyone could explain how to do this, it would really help.

应用示例

You can set edit-view's value in text-view of selected position through adapter at onClick event listener.

For that what you need is array of texts for display in text-view .

In case of Edit

  1. In onclick of any item of recyclerView show editText and add fact button.
  2. In your on Click of Add Fact Button: get the text from editText and set it to array via:
 arraylist.set(pos,text); adapter.notifyItemChanged(pos); 

In case of Add In your onClick of Add Fact Button: get the text from editText and set it to array via:

 arraylist.add(text); adapter.notifyItemInserted(arraylist.size()-1); 

@Michael Dadi -If anyone could explain how to do this, it would really help.

I just explain you to way to archive this task

First of all, make POJO or Model class from JSON it will be an easy

{


 "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"
        }
      ],

}


],

}

Here is a model class from 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;
        }
    }
}

}

it will store your data temporary while app close u will lost all data so you can use any database for saving data in the local device.

now you can set adapter using this Animal.class in recyclerview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM