I have a string with a format that looks like this:
Recipe{id=someID, title=someTitle, image='LINK', usedIngredientCount=SomeNumber, missedIngredientCount=SomeNumber2, likes=SomeNumber3}Recipe{id=someID, title=someTitle, image='LINK', usedIngredientCount=SomeNumber, missedIngredientCount=SomeNumber2, likes=SomeNumber3}
Note that in the example above the string contains 2 Recipes, but it can actually contain any number of recipes. I want to put whatever number of recipes I have in a RecyclerView, and I am wondering what is the easiest way of doing it. What I am thinking is splitting the main string into substrings everytime I find the word Recipe
then extracting someID
LINK
SomeNumber1
SomeNumber2
SomeNumber3
from every substring and finally populating the RecyclerView using these values.
Could you please help me either convert my idea into code or come up with easier ways of doing it?
Thank you so much
class MainActivity extends AppCompatActivity
{
public RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public RecyclerAdapter adapterD;
ArrayList<String> id;
ArrayList<String> title;
ArrayList<String> image;
ArrayList<String> usedIngredientCount;
ArrayList<String> missedIngredientCount;
ArrayList<String> likes;
void onCreate()
{
id= new ArrayList<>();
title = new ArrayList<>();
image = new ArrayList<>();
likes = new ArrayList<>();
usedIngredientCount= new ArrayList<>();
missedIngredientCount = new usedIngredientCountArrayList<>();
// your recipe string here
String recipe = "id title image usedIngredientCount missedIngredientCount likes" ;
// break the string
dataSplit(recipe)
// call the recyclerview adapter
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapterD = new RecyclerAdapter(MainActivity.this, id, title, image,
usedIngredientCount, missedIngredientCount, likes);
recyclerView.setAdapter(adapterD);
}
void dataSplit(String recipe)
{
// Split String when there is space
String parts[] = recipe.split(" ");
id.add( parts[0] );
title.add(parts[1]);
image.add(parts[2]);
usedIngredientCount.add(parts[3]);
missedIngredientCount.add(parts[4]);
likes.add(parts[5]);
}
}
}
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.