如何在sqlite android片段中更改或删除数据时刷新recyclelerview?

时间:2018-03-28 12:39:56

标签: java android sqlite android-fragments android-sqlite

我在SQLite android中存储标题,缩略图,id并在recyclerview中显示。 我创建了所有方法,即在我的sqlite帮助器类中获取数据,添加,删除和更新,并在我的片段android中调用这些方法。

但问题是当我删除或添加项目到sqlite时,它没有刷新。 是的,我在片段类中定义了adapter.notifydatasetChanged()

示例:

这是我的sqlite openhelper类:

  1. public List<dataModel> getDatabase();
  2. public void addData(dataModel data);
  3. public void deleteData(dataModel data);
  4. 我在片段类中调用这些方法,而我正在分配  getDatabase()列在片段中。

    提前致谢。 编辑1:

    public class LightFragment extends Fragment {
    
    RecyclerView mRecyclerView;
    FloatingActionButton addLightTitle,allLightsOff;
    ArrayList<LightTitle> lightTitle;
    RecyclerView.LayoutManager mLayoutManager;
    
    EditText edxLightName;
    Button btnLightName;
    
    SQLiteDatabase  db;
    LDbHelper lHelper;
    ArrayList<LightPojo> mLightPojos = new ArrayList<>();
    LightRecyclerView li;
    
    
    @SuppressLint("ValidFragment")
    public LightFragment(Context mContext) {
    
        lHelper = new LDbHelper(mContext);
    
    }
    
    public LightFragment() {
    }
    
    
    
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_light, container, false);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mLightPojos = lHelper.getLightData();
    
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        mRecyclerView = view.findViewById(R.id.light_frag);
        addLightTitle = view.findViewById(R.id.fab_add_light_title);
        allLightsOff = view.findViewById(R.id.fab_off_all_lights);
    
        mLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        mLightPojos = lHelper.getLightData();
               li = new LightRecyclerView(getContext(),mLightPojos);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(li);
        li.notifyDataSetChanged();
    
        mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),mRecyclerView,new ClickListener(){
    
            @Override
            public void onClick(View view, int position) {
                Log.d("BT","clicked" + mLightPojos.get(position).getName());
                //toast("clicked" + position );
    
                if (!mLightPojos.get(position).isClicked()){
                    ImageView lightPopUp = view.findViewById(R.id.row_light_thumbnail);
                    lightPopUp.setImageResource(R.drawable.light_on);
                    //DrawableCompat.setTint(lightPopUp.getDrawable().mutate(), ContextCompat.getColor(getContext(), R.color.white));
                    toast(mLightPojos.get(position).getName() + "light on");
                    Log.d("BT","clicked" + mLightPojos.get(position).isClicked());
                    //mLightPojos.get(position).setClicked(true);
                    LightPojo pojo = new LightPojo(mLightPojos.get(position).getId(),R.drawable.light_on,true);
                    lHelper.upDateState(pojo);
                }else {
                    ImageView lightPopUp = view.findViewById(R.id.row_light_thumbnail);
                    toast(mLightPojos.get(position).getName() + "light off");
                    lightPopUp.setImageResource(R.drawable.light_off);
                    //DrawableCompat.setTint(lightPopUp.getDrawable().mutate(), ContextCompat.getColor(getContext(), R.color.colorAccent));
                    Log.d("BT","clicked" + mLightPojos.get(position).isClicked());
                    //mLightPojos.get(position).setClicked(false);
                    //lHelper.upDateState(position,R.drawable.light_off,false);
                    LightPojo pojo = new LightPojo(mLightPojos.get(position).getId(),R.drawable.light_off,false);
                    lHelper.upDateState(pojo);
                }
    
    
    
            }
    
            @Override
            public void onLongClick(View view, final int position) {
                toast("long pressed at " + position);
                final Dialog eDialog = new Dialog(getActivity());
                eDialog.setContentView(R.layout.edit_light_name);
                //EditText editLightName = eDialog.findViewById(R.id.edit_light_name_edx);
                //final String editedLightName = editLightName.getText().toString();
                Button deleteLightName = eDialog.findViewById(R.id.btn_delete_light_name);
                Button updateLightName = eDialog.findViewById(R.id.btn_update_light_name);
                eDialog.show();
               // eDialog.setTitle("Edit light name");
    
    
    
                updateLightName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //lightTitle.set(position,editLightName.getText().toString());
                        //Log.d("BT",editedLightName);
                        EditText editLightName = eDialog.findViewById(R.id.edit_light_name_edx);
    
                        //lightTitle.set(position,new LightTitle(editLightName.getText().toString(),false));
                        LightPojo pojo = new LightPojo(mLightPojos.get(position).getId(),editLightName.getText().toString());
                        lHelper.editName(pojo);
                        lHelper.getLightData();
                        li.notifyDataSetChanged();
                        eDialog.dismiss();
                    }
                });
    
                deleteLightName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //lightTitle.remove(position);
                        LightPojo pojo = new LightPojo(mLightPojos.get(position).getId());
                        lHelper.delete(pojo);
                        li.notifyItemChanged(position);
                        //li.notifyDataSetChanged();
                        eDialog.dismiss();
                    }
                });
    
    
            }
        }));
    
        addLightTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //toast("clicked fab button");
    
                //LightTitleDialog titleDialog = new LightTitleDialog();
                //titleDialog.show(getActivity().getFragmentManager(),"title");
                final Dialog mDialog = new Dialog(getActivity());
                mDialog.setTitle("Add light name");
                mDialog.setContentView(R.layout.add_light_title);
                edxLightName = mDialog.findViewById(R.id.edt_light_title);
                btnLightName = mDialog.findViewById(R.id.btn_add_title);
                mDialog.show();
    
                btnLightName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //toast("Add light");
                        if (edxLightName.getText().length() != 0){
                            String newLightName = edxLightName.getText().toString();
                            //lightTitle.add(new LightTitle(newLightName,false));
                            //add(newLightName,R.id.utilities_thumbnail,false);
                            lHelper.insertData(newLightName,R.drawable.light_off,false);
                            li.notifyDataSetChanged();
                            mDialog.dismiss();
                        }else {
                            toast("Please enter light name");
                        }
                    }
                });
    
    
            }
        });
    

3 个答案:

答案 0 :(得分:0)

更新数据模型后,请调用以下方法。

 adapter.notifyItemRemoved(position);

这将更新适配器有关从列表中删除元素的信息。

答案 1 :(得分:0)

更新数据模型后,从Fragment或Activity调用adapter.notifydatasetChanged()(无论您使用什么)。然后将更新recyclerView。

答案 2 :(得分:0)

是的,我发现单击时我必须显式更改ArrayList。除了数据库更新。

将值更新到SQLite调用适配器方法以进行内部更新值之后。每次更新数据时都不需要获取SQLite数据。