Android:无法强制转换为java.lang.CharSequence

Android:无法强制转换为java.lang.CharSequence,android,sqlite,android-recyclerview,Android,Sqlite,Android Recyclerview,我不知道我在这方面哪里出了问题,我正在尝试在一个回收视图中列出一个食谱的成分,我只是无法让onBindViewHolder工作,不管我尝试了什么。下面是适配器代码以及xml Recipe.class。本质上,我需要向recyclerView显示Recipe类的结果 配方类 public class Recipe extends MainActivity { TabLayout tabLayout; ImageView recipeImage; TextView desc

我不知道我在这方面哪里出了问题,我正在尝试在一个回收视图中列出一个食谱的成分,我只是无法让onBindViewHolder工作,不管我尝试了什么。下面是适配器代码以及xml Recipe.class。本质上,我需要向recyclerView显示Recipe类的结果

配方类

public class Recipe extends MainActivity {

    TabLayout tabLayout;
    ImageView recipeImage;
    TextView descriptionText, courseText, servingsText, costText, caloriesText, methodText;
    RecyclerView listIngredient;
    SQLiteDatabase db;
    String search_name;
    Cursor c;
    RecyclerViewAdapter adapterRecipe;
    List<RecipeList> itemRecipe = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipe);
        //search_name = getIntent().getStringExtra("NAME");
        search_name = "Speedy chicken couscous";

        loadRecipe();
        //recyclerview Recipe
        adapterRecipe = new RecyclerViewAdapter(this, itemRecipe);
        listIngredient = findViewById(R.id.listIngredient);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        listIngredient.setLayoutManager(mLayoutManager);
        listIngredient.setItemAnimator(new DefaultItemAnimator());
        listIngredient.setAdapter(adapterRecipe);

    }
    public void loadRecipe() {
        itemRecipe.clear();
        db = (new DatabaseManager(this).getWritableDatabase());
        String RECIPE_SEARCH = " SELECT A.recipe, A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
                "FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
                " AS B ON A.ingredient = B.ingredient_name";
        String selectQuery = "";
        selectQuery = RECIPE_SEARCH + " WHERE A.recipe LIKE ?";
        c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
        if (c.moveToFirst()) {
            do {
                RecipeList recipeList = new RecipeList();
                recipeList.setRecipe(c.getString(c.getColumnIndex("recipe")));
                recipeList.setIngredient_amount(c.getString(c.getColumnIndex("ingredient_quantity")));
                recipeList.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
                recipeList.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
                recipeList.setDescription(c.getString(c.getColumnIndex("description")));
                itemRecipe.add(recipeList);
            } while (c.moveToNext());
            c.close();
        }

    }
}
互惠列表模型 公共类接受者{

    private Integer id;
    private Double ingredient_quantity;
    private String recipe;
    private String measurement_name;
    private String ingredient_name;
    private String description;
    private String ingredient_amount = String.valueOf(ingredient_quantity);
    public RecipeList(){

    }

    public RecipeList(String recipe, String ingredient_amount, String measurement_name, String ingredient_name, String description) {

        this.recipe = recipe;
        this.ingredient_amount = ingredient_amount;
        this.measurement_name = measurement_name;
        this.ingredient_name = ingredient_name;
        this.description = description;
    }

    public String getRecipe() {
        return recipe;
    }

    public void setRecipe(String recipe) {
        this.recipe = recipe;
    }

    public String getIngredient_amount() {
        return ingredient_amount;
    }

    public void setIngredient_amount(String ingredient_amount) {
        this.ingredient_amount = ingredient_amount;
    }

    public String getMeasurement_name() {
        return measurement_name;
    }

    public void setMeasurement_name(String measurement_name) {
        this.measurement_name = measurement_name;
    }

    public String getIngredient_name() {
        return ingredient_name;
    }

    public void setIngredient_name(String ingredient_name) {
        this.ingredient_name = ingredient_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
更新错误

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<RecipeList> itemRecipe;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
RecyclerViewAdapter(Context context, List<RecipeList> data) {
    this.mInflater = LayoutInflater.from(context);
    this.itemRecipe = data;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.fragment_item, parent, false);
    return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.myTextView.setText(itemRecipe.get(position));

}

// total number of rows
@Override
public int getItemCount() {
    return itemRecipe.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView = itemView.findViewById(R.id.listIngredient);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
    }
}


// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
    this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
    void onItemClick(View view, int position);
}
尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

在这一行:
holder.myTextView.setText(itemRecipe.get(position).toString());

在代码
itemRecipe.get(position)
中返回
RecipeList
此对象您将RecipeList直接传递给setText,setText需要字符串,请尝试在此处传递字符串值
holder.myTextView.setText(//传递字符串)

将RecipeList作为字符串重写传递给RecipeList中的字符串

示例:这里添加了示例格式,并包含了toString中的所有变量

@Override
public String toString() {
    return "RecipeList{" +
            "id=" + id +
            ", ingredient_quantity=" + ingredient_quantity +
            ", recipe='" + recipe + '\'' +
            ", measurement_name='" + measurement_name + '\'' +
            ", ingredient_name='" + ingredient_name + '\'' +
            ", description='" + description + '\'' +
            ", ingredient_amount='" + ingredient_amount + '\'' +
            '}';
}
调用setText中RecipeList对象上的toString

例:


发布带有问题的错误日志。花点时间阅读以下内容:谢谢,我如何将字符串值放在这里?我已经实现了您的建议,我得到了“无法解析符号'toString'”。我在RecipeList模型中使用了toString方法。我在RecipeList中使用了toString方法。类此解决方案解决了我遇到的问题,现在我面临的问题是,它什么都不做,只是停止运行。
    private Integer id;
    private Double ingredient_quantity;
    private String recipe;
    private String measurement_name;
    private String ingredient_name;
    private String description;
    private String ingredient_amount = String.valueOf(ingredient_quantity);
    public RecipeList(){

    }

    public RecipeList(String recipe, String ingredient_amount, String measurement_name, String ingredient_name, String description) {

        this.recipe = recipe;
        this.ingredient_amount = ingredient_amount;
        this.measurement_name = measurement_name;
        this.ingredient_name = ingredient_name;
        this.description = description;
    }

    public String getRecipe() {
        return recipe;
    }

    public void setRecipe(String recipe) {
        this.recipe = recipe;
    }

    public String getIngredient_amount() {
        return ingredient_amount;
    }

    public void setIngredient_amount(String ingredient_amount) {
        this.ingredient_amount = ingredient_amount;
    }

    public String getMeasurement_name() {
        return measurement_name;
    }

    public void setMeasurement_name(String measurement_name) {
        this.measurement_name = measurement_name;
    }

    public String getIngredient_name() {
        return ingredient_name;
    }

    public void setIngredient_name(String ingredient_name) {
        this.ingredient_name = ingredient_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
@Override
public String toString() {
    return "RecipeList{" +
            "id=" + id +
            ", ingredient_quantity=" + ingredient_quantity +
            ", recipe='" + recipe + '\'' +
            ", measurement_name='" + measurement_name + '\'' +
            ", ingredient_name='" + ingredient_name + '\'' +
            ", description='" + description + '\'' +
            ", ingredient_amount='" + ingredient_amount + '\'' +
            '}';
}
holder.myTextView.setText(itemRecipe.get(position).toString)