简体   繁体   English

RecyclerView不显示任何内容

[英]The RecyclerView does not show anything

I want a list of items to be displayed in a List using RecyclerView, I have tried hard but could not find a mistake in my code. 我希望使用RecyclerView在列表中显示项目列表,我已经尽力了,但是在代码中找不到错误。 The recyclerView does not show up !! recyclerView没有显示!

here is the code of my activity which contains the recycler view 这是我的活动代码,其中包含回收者视图

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;
       }
     }

Here is it's XML file 这是XML文件

         <android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/ingredientList"
    />

Here is my adapter 这是我的适配器

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);
    }
 }
}

Here is the XML to be adapted 这是要适应的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>

Look on this line in getData() : 看这行在getData()

for(int i = 0;i<data.size();i++){

If you still don't see, where is your issue, make from it: 如果仍然看不到,问题出在哪里,请从中进行查找:

for(int i = 0;i<list.length;i++){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM