0

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.

App Example

Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
Michael Dadi
  • 362
  • 3
  • 12
  • check this https://stackoverflow.com/questions/51454613/how-can-i-validate-recyclerview-adapter-textinputedittext-from-fragment/51454770#51454770 && https://stackoverflow.com/a/47975852/7666442 – AskNilesh Mar 01 '19 at 04:53
  • 1
    show your java code – Suraj Vaishnav Mar 01 '19 at 04:56
  • @Michael Dadi you can save Edittext value on Add button click. You will get an adaptor position on a button click, so as per the position you get your id to save data. – Hemant N. Karmur Mar 01 '19 at 04:57
  • @Michael Dahi from above small description i can say is do like : simply add to your data structure ( mItems ) , and then notify your adapter about dataset change private void addItem(String item) { mItems.add(item); mAdapter.notifyDataSetChanged(); } and for more help you need post your try over here – Developer Mar 01 '19 at 04:58

2 Answers2

0

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);
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

@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.

Adil
  • 812
  • 1
  • 9
  • 29