-1

I have a recyclerview like this:

item 10
admob (item 9 has gone)
item 8
item 7
item 6 ...

I'm adding admob in position 2. It is what I want.

The problem is to parse the data. I want to jump the position 2 when parse the data, like so:

item 10
admob
item 9 <-- now I have item 9
item 8
item 7
item 6 ...

my parse data:

//This method will parse json data
private void parseData(JSONArray array) {
    for (int i = 0; i < array.length(); i++) {
        //Creating the object
        Posts PostObj = new Posts();
        JSONObject json;
        try {
            //Getting json
            json = array.getJSONObject(i);

            //Adding data to the object
            PostObj.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
            PostObj.setName(json.getString(Config.TAG_NAME));
...

Any ideas how to jump parsing in position 2 that is for my admob?

I tried to add if(i == 2) { return; } but all itens disappear...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
RGS
  • 4,062
  • 4
  • 31
  • 67

2 Answers2

1

You have to do something like this

//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
    if(i!=1){
      //Creating the object
      Posts PostObj = new Posts();
      JSONObject json;
      try {
          //Getting json
          json = array.getJSONObject(i);

          //Adding data to the object
          PostObj.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
          PostObj.setName(json.getString(Config.TAG_NAME));
          ...
    }
  ...

if you want to skip the 2nd element you need to check if i is equal to 1, not 2, 2 is the 3rd element and, if you execute a return you exit from the method, so you only have to not execute the code if you have the 2nd item

Ivan Notarstefano
  • 115
  • 1
  • 4
  • 18
1

You can try doing something like this

for (int i = 0 ; i < n ; i++) {
    if (i == 1) {
        // todo: addmob stuff;
    }
    // todo: add object to recycler view
}
Shiny Das
  • 46
  • 5