簡體   English   中英

RecyclerView不顯示任何內容

[英]The RecyclerView does not show anything

我希望使用RecyclerView在列表中顯示項目列表,我已經盡力了,但是在代碼中找不到錯誤。 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>

看這行在getData()

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

如果仍然看不到,問題出在哪里,請從中進行查找:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM