我想要使用RecyclerView在List中显示的项目列表,我已经努力但在我的代码中找不到错误。 recyclerView没有显示!!
这是我的活动代码,其中包含回收者视图
public class SubIngredients extends AppCompatActivity {
private RecyclerView ingredientList;
private SubIngredientAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_ingredients);
ingredientList = (RecyclerView) findViewById(R.id.ingredientList);
ingredientList.setLayoutManager(new LinearLayoutManager(this));
adapter = new SubIngredientAdapter(getApplicationContext(),getData());
ingredientList.setAdapter(adapter);
}
public List<values> getData(){
List<values> data = new ArrayList<>();
String []list = {"Potato", "Tomato", "Orange", "Apple","Carrot", "Lady Finger", "Spinach", "Pumpkin",
"Pea","Mango","Banana"};
for(int i = 0;i<data.size();i++){
values val = new values(list[i]);
data.add(val);
}
return data;
}
}
这是它的XML文件
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/ingredientList"
/>
这是我的适配器
public class SubIngredientAdapter extends RecyclerView.Adapter<SubIngredientAdapter.SubIngredientViewHolder> {
private LayoutInflater inflater;
List<values> data;
private SubIngredientViewHolder viewHolder;
public SubIngredientAdapter(Context context, List<values> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public SubIngredientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View viewGroup = inflater.inflate(R.layout.ingredient_container,parent,false);
viewHolder = new SubIngredientViewHolder(viewGroup);
return viewHolder;
}
@Override
public void onBindViewHolder(SubIngredientViewHolder holder, int position) {
values ingredient = data.get(position);
Log.d("","Bind");
viewHolder.ingredientText.setText(ingredient.value);
}
@Override
public int getItemCount() {
return data.size();
}
class SubIngredientViewHolder extends RecyclerView.ViewHolder{
TextView ingredientText;
ImageView image;
public SubIngredientViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.arrow_right);
ingredientText = (TextView) itemView.findViewById(R.id.ingredient_name);
}
}
}
以下是要改编的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/verylightRed"
android:layout_marginTop="@dimen/ingredientRelativeContainerMarginTop"
android:layout_height="@dimen/ingredientRelativeContainerHeight"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Something"
android:id="@+id/ingredient_name"
android:layout_marginLeft="@dimen/ingredientMarginLeft"
android:textColor="@color/ingredientTextColor"
android:textSize="@dimen/ingredientTextSize" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/arrow_right"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:src="@drawable/right_arrow" />
</RelativeLayout>
答案 0 :(得分:0)
在getData()
中查看此行:
for(int i = 0;i<data.size();i++){
如果你仍然没有看到,你的问题在哪里,请从中做出:
for(int i = 0;i<list.length;i++){