javaandroidsqlitesugarorm

String null after object creating in constructor


I'm passing String to RecyclerView.Adapter in order to use it as Database query parameter, but stranger things, I'm getting this String as null.

I tried to use not static String, but it didn't help

String from Activity is passed to Adapter correctly and isn't null, but in Adapter there is something strange.

Could you please advise what's wrong?

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

Activity:

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) 

Solution

  • make this changes into your code.

    1. remove query part from adapter also adapter handle only bind there for your recycler view work faster.logical part also in activity or fragment.

    adapater change..

     List<Food> foodDataList = new ArrayList<>();// hear you can pass any pojo class object.
    Context mContext;
    
    public FoodAdapter(List<Food> foodDataList, Context mContext) {
        this.foodDataList = foodDataList;
        this.mContext = mContext;
    }
    

    and in main activity..

       List<Food> foodList=db.readFoodData(); // here your database read opration and give list of food
        FoodAdapter foodAdapterSandwich = new FoodAdapter(foodList,this);
    
    
        RecyclerView foodRecyclerView = findViewById(R.id.sandwich_recycler_view);
        foodRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    
        foodRecyclerView.setAdapter(foodAdapterSandwich);
        foodAdapterSandwich.notifyDataSetChanged();
    

    and make sure your database query work and it give list of food data.