当前位置: 首页 > 知识库问答 >
问题:

如何删除项目/仪器MVVM

籍英叡
2023-03-14

我是编程和Android的新手,有一个问题,所以我必须使用MVVM、LiveData、RecyclerView和Fragments构建一个Android应用程序(购买汽车的应用程序)。我可以创建一个汽车实例,然后在RecyclerView中查看,但是当我想删除它时,我会收到以下错误:

Logcat:



    java.lang.NullPointerException: **Attempt to invoke virtual method 'void com.example.da.ViewModel.MyViewModel.deleteCar(com.example.da.Model.Car)' on a null object reference**
            at com.example.da.Adapter.RecyclerAdapter$MyViewHolder$2.onClick(RecyclerAdapter.java:127)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:223)
            at android.app.ActivityThread.main(ActivityThread.java:7656)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    

我希望有人能帮助我。非常感谢!

<pre><code>
@Dao
public interface CarDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void addCar( Car car);

    @Query("SELECT * FROM Car ORDER BY model ASC")
     LiveData<List<Car>>getAllCars();

    @Delete
    public void  deleteCar(Car car);

    @Query("DELETE FROM Car")
    void deleteAll();

    @Update
    void updateCar(Car car);
    
    //User
    @Insert
    void registerUser(User user);

}
</pre></code>

数据库

<pre><code>
@Database(entities = {Car.class, User.class},version =1, exportSchema = false)

public abstract class RoomDatabase extends androidx.room.RoomDatabase {

    private static RoomDatabase roomDatabase;
    private static final String databaseName="user";
    // Dao including
    public abstract CarDao carDao();
    public abstract UserDao userDao();


    public static synchronized RoomDatabase getUserDatabase(Context context)
    {
        if (roomDatabase==null)
        {
            roomDatabase= Room.databaseBuilder(context, RoomDatabase.class,databaseName)
                    .fallbackToDestructiveMigration()
                    .build();
        }
        return roomDatabase;
</pre></code>

存储库

<pre><code>

public class Repository {
   private CarDao carDao;
   private LiveData<List<Car>>getAllCars;

    public Repository(Application application)
    {
        RoomDatabase db = RoomDatabase.getUserDatabase(application);
        carDao=db.carDao();
        getAllCars=carDao.getAllCars();

    }
    //CarDao methods
    public void addCar( Car car)
    {
       new newAsyncTask(carDao).execute(car);

    }

    private static class newAsyncTask extends AsyncTask<Car, Void, Void>{
        private CarDao myAsyncDao;

        newAsyncTask(CarDao carDao){
            myAsyncDao=carDao;
        }

        @Override
        protected Void doInBackground(Car... cars) {
            myAsyncDao.addCar(cars[0]);
            return null;
        }
    }

    public void updateCar( Car car)
    {
        new updateAsyncTask(carDao).execute(car);

    }

    private static class updateAsyncTask extends AsyncTask<Car, Void, Void>{
        private CarDao myAsyncDao;

        updateAsyncTask(CarDao carDao){
            myAsyncDao=carDao;
        }

        @Override
        protected Void doInBackground(Car... cars) {
            myAsyncDao.updateCar(cars[0]);
            return null;
        }
    }

    public void deleteCar( Car car)
    {
        new deleteAsyncTask(carDao).execute(car);

    }

    private static class deleteAsyncTask extends AsyncTask<Car, Void, Void>{
        private CarDao myAsyncDao;

        deleteAsyncTask(CarDao carDao){
            myAsyncDao=carDao;
        }

        @Override
        protected Void doInBackground(Car... cars) {
            myAsyncDao.deleteCar(cars[0]);
            return null;
        }
    }
</pre></code>

回收器适配器

<pre><code>
    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

   private List<Car>list;
    Context context;
   private MyViewModel myViewModel;
   
    public RecyclerAdapter(List<Car> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        context=parent.getContext();
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Car car = list.get(position);
        holder.tvModel.setText(car.getCarModel());
        holder.tvColor.setText(car.getCarColor());
        holder.tvDpl.setText(car.getCarDpl());
        holder.tvDescription.setText(car.getCarDescription());
        holder.tvPrice.setText(car.getCarPrice());
    }

    @Override
    public int getItemCount() {
        if(list !=null)
        {
            return list.size();
        }
        else return 0;
    }

   public void setData(List<Car>list){
       this.list=list;
       notifyDataSetChanged();
   }

    // generate ViewHolder
    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvModel;
        TextView tvColor;
        TextView tvDpl;
        TextView tvDescription;
        TextView tvPrice;
        Button btnDelete;

        // Constructor of ViewHolder
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            // Inflate the views
            tvModel=itemView.findViewById(R.id.model);
            tvColor=itemView.findViewById(R.id.color);
            tvDescription=itemView.findViewById(R.id.description);
            tvDpl=itemView.findViewById(R.id.dpl);
            tvPrice=itemView.findViewById(R.id.price);
            btnDelete=itemView.findViewById(R.id.btn_delete);

            // Listener for Delete button
            btnDelete.setOnClickListener(this);
            btnDelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {


                    CreateAlertDialoge();

                }
            });

        }

        private void CreateAlertDialoge()
        {
            AlertDialog.Builder builder =new AlertDialog.Builder(context);
            builder.setMessage("Are you sure to delete");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Car car = new Car();
                    int ID=list.get(getAdapterPosition()).getCarId();
                    car.setCarId(ID);
                   // if (car==null){
                   //     return;
                   // }

                   //MainActivity.roomDatabase.carDao().deleteCar(car);

                   myViewModel.deleteCar(car);

                    MainActivity.fragmentManager.beginTransaction().replace(R.id.container,new BaseFragment(),null).commit();

                    Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context, "no", Toast.LENGTH_SHORT).show();

                }
            });
            builder.create();
            builder.show();
        }

        @Override
        public void onClick(View v)
        {
            Car car = new Car();
            int ID=list.get(getAdapterPosition()).getCarId();
            car.setCarId(ID);

            //MainActivity.roomDatabase.carDao().deleteCar(car);
           myViewModel.deleteCar(car);
            MainActivity.fragmentManager.beginTransaction().replace(R.id.container,new BaseFragment(),null).commit();
        }
    }


}
</pre></code>

视图模型

<pre><code>
    public class MyViewModel extends AndroidViewModel {
   private Repository repository;
   private LiveData<List<Car>>getAllCars;

    public MyViewModel(@NonNull Application application) {
        super(application);
        repository=new Repository(application);
        getAllCars=repository.getAllCars();
    }
   public  LiveData<List<Car>>getGetAllCars(){
        return getAllCars;
    }

    public void addCar( Car car)
    {
        repository.addCar(car);
    }
    public void updateCar(Car car){
        repository.updateCar(car);
    }
    public void registerUser( User user)
    {
        repository.registerUser(user);
    }

    public void  deleteCar(Car car)
    {
        repository.deleteCar(car);
    }
    public void deleteAll(){
        repository.deleteAll();
    }

}
</pre></code>

共有1个答案

欧阳骏俊
2023-03-14

这里的主要问题是RecyclAdapter中的myViewModel从未设置过值。您可以在实例化RecyclAdapter时传递ViewModel的实例。但是,这被认为是一个坏主意。

一个更好的方法是创建一个回调,从RecyclAdapter返回到您的片段。请参见:https://stackoverflow.com/a/53466188/9656117

 类似资料:
  • 我已经在GitLab中创建了几个存储库。其中一个是为了测试目的,有一些提交和分支。我想删除或删除此存储库。我该怎么做呢?

  • 本文向大家介绍Android studio 如何删除项目 module,包括了Android studio 如何删除项目 module的使用技巧和注意事项,需要的朋友参考一下 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: Android Studio对module做了一个保护机制,module是不能随

  • 有什么方法可以立即删除GCP项目吗?

  • 从项目中去除可可的正确方法是什么?我要把整块可可皮去掉。由于我的客户施加的一些限制,我不能使用它。我只需要一个xcodeproj而不是一个XCWorkspace。

  • 问题内容: 假设我使用以下光标获取某人的通话记录: 现在我将如何删除该光标中的第i个项目?这也可能是获取音乐等信息的光标。因此,我必须问-这是否可能?对于某些游标,我可以理解为不允许删除第三方应用程序。 谢谢。 问题答案: 对不起,您不能从光标中删除。 您必须使用ContentResolver或某种SQL调用。