在构造函数中创建对象后的字符串 null

String null after object creating in constructor

我将字符串传递给 RecyclerView.Adapter 以便将其用作数据库查询参数,但奇怪的是,我将此字符串作为空值获取。

我试过使用 not static String,但没用

来自 Activity 的字符串已正确传递给适配器并且不为空,但在适配器中有一些奇怪的东西。

请问有什么问题吗?

适配器:

  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) 

在您的代码中进行此更改。

  1. 从适配器中删除查询部分,适配器句柄也只绑定在那里以供您的回收者查看工作faster.logical部分也在activity或片段中。

适配器更改..

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

和主要 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();

并确保您的数据库查询有效并且它提供食物数据列表。