Java 从字符串获取数据并填充RecyclerView
我有一个格式如下的字符串:Java 从字符串获取数据并填充RecyclerView,java,android,android-recyclerview,Java,Android,Android Recyclerview,我有一个格式如下的字符串: Recipe{id=someID, title=someTitle, image='LINK', usedIngredientCount=SomeNumber, missedIngredientCount=SomeNumber2, likes=SomeNumber3}Recipe{id=someID, title=someTitle, image='LINK', usedIngredientCount=SomeNumber, missedIngredientCount
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}
请注意,在上面的示例中,字符串包含2个配方,但它实际上可以包含任意数量的配方。
我想把任意数量的食谱放在一个回收视图中,我想知道最简单的方法是什么。我的想法是每次我找到单词Recipe
时将主字符串拆分为子字符串,然后从每个子字符串中提取someID
链接SomeNumber1
SomeNumber2
SomeNumber3
,最后使用这些值填充RecyclerView
你能帮我把我的想法转换成代码或者想出更简单的方法吗
非常感谢您class MainActivity扩展了AppCompatActivity
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]);
}
}
}
{
公共回收视图回收视图;
private RecyclerView.LayoutManager LayoutManager;
公共回收设备适配器;
ArrayList id;
ArrayList标题;
阵列列表图像;
arraylistusedingentcount;
ArrayList MissingRedientCount;
ArrayList喜欢;
void onCreate()
{
id=新的ArrayList();
title=新的ArrayList();
image=newarraylist();
likes=newarraylist();
usedIngredientCount=new ArrayList();
MissingRedientCount=新建usedIngredientCountArrayList();
//你的食谱串在这里
String recipe=“id title image usedIngredientCount missdingredientcount likes”;
//断线
数据片段(配方)
//调用recyclerview适配器
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
layoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapterD=新的回收器适配器(MainActivity.this、id、标题、图像、,
usedIngredientCount、MissingRedientCount、likes);
recyclerView.setAdapter(adapterD);
}
无效数据片段(字符串配方)
{
//有空格时拆分字符串
字符串部分[]=配方。拆分(“”);
id.add(部分[0]);
标题.添加(第[1]部分);
添加(第[2]部分);
使用redientcount.add(部分[3]);
MissingRedientCount.add(第[4]部分);
添加(第[5]部分);
}
}
}
谢谢,但是如果字符串的格式是这样的话,这行得通吗“id title image usedIngredientCount missedIngredientCount likes”
但是我的字符串有以下格式:Recipe{id=someID,title=someTitle,image='LINK',usedIngredientCount=SomeNumber,missedIngredientCount=SomeNumber2,likes=SomeNumber3}Recipe{id=someID,title=someTitle,image='LINK',usedIngredientCount=SomeNumber,missdingredientcount=SomeNumber2,likes=SomeNumber3}