Я передаю String в RecyclerView.Adapter, чтобы использовать его в качестве параметра запроса к базе данных, но, как ни странно, я получаю эту строку как ноль.
Я пытался использовать не статическую строку, ноэто не помогло
Строка из Activity передается в Adapter правильно и не имеет значения null, но в Adapter есть что-то странное.
Не могли бы вы сообщить, что не так?
Адаптер:
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {
public FoodAdapter(String s) {
foodDataList = Food.findWithQuery(Food.class, "Select * from Food where type = ?", s);
}
public class FoodViewHolder extends RecyclerView.ViewHolder {
private ImageView food_image;
private TextView name;
private TextView description;
private TextView price;
public FoodViewHolder(View itemView) {
super(itemView);
food_image = itemView.findViewById(R.id.food_image);
name = itemView.findViewById(R.id.name);
description = itemView.findViewById(R.id.description);
price = itemView.findViewById(R.id.price);
}
public void bind(Food food) {
name.setText(food.getName());
description.setText(food.getDescription());
price.setText("Цена: " + String.valueOf(food.getPrice()));
Picasso.get().load(String.valueOf(food.getPicture())).into(food_image);
}
}
@Override
public FoodAdapter.FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new FoodViewHolder(view);
}
@Override
public void onBindViewHolder(FoodAdapter.FoodViewHolder holder, int position) {
holder.bind( foodDataList.get(position));
}
@Override
public int getItemCount() {
return foodDataList.size();
}
}
Активность:
package com.kosenin.boston.bronxcafe.View;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.kosenin.boston.bronxcafe.Model.BackendlessData;
import com.kosenin.boston.bronxcafe.Presenter.FoodAdapter;
import com.kosenin.boston.bronxcafe.R;
public class SandwichActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sandwich);
Intent intent = getIntent();
String FOODTYPE = intent.getStringExtra("TypeSandwich");
FoodAdapter foodAdapterSandwich = new FoodAdapter(FOODTYPE);
RecyclerView foodRecyclerView = findViewById(R.id.sandwich_recycler_view);
foodRecyclerView.setLayoutManager(new LinearLayoutManager(this));
foodRecyclerView.setAdapter(foodAdapterSandwich);
}
}
Logcat:
Process: com.kosenin.boston.bronxcafe, PID: 10323
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kosenin.boston.bronxcafe/com.kosenin.boston.bronxcafe.View.SandwichActivity}: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.orm.SugarRecord.findWithQuery(SugarRecord.java:181)
at com.kosenin.boston.bronxcafe.Presenter.FoodAdapter.<init>(FoodAdapter.java:34)
at com.kosenin.boston.bronxcafe.View.SandwichActivity.onCreate(SandwichActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)