当sqlite android片段中的数据更改或删除时如何刷新recyclerview?
Posted
技术标签:
【中文标题】当sqlite android片段中的数据更改或删除时如何刷新recyclerview?【英文标题】:How to refresh recyclerview when data changed or removed in sqlite android fragments? 【发布时间】:2018-09-07 03:46:15 【问题描述】:我将标题、缩略图、ID 存储在 SQLite android 中并显示在 recyclerview 中。 我已经创建了所有方法,即在我的 sqlite 助手类中获取数据、添加、删除和更新,并在我的片段 android 中调用这些方法。
但问题是当我删除或添加项目到 sqlite 它不刷新。
是的,我在片段类中定义了adapter.notifydatasetChanged()
。
示例:
这是我的 sqlite openhelper 类:
public List<dataModel> getDatabase();
public void addData(dataModel data);
public void deleteData(dataModel data);
我在片段类中调用这些方法并且我正在分配
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");
);
);
【问题讨论】:
你只需要在调用notifyDataSetChanged()
之前修改数据库。不过,您永远不会为Adapter
更新ArrayList
。
对不起,我没听懂。
您的 Adapter
– LightRecyclerView
– 正在从 mLightPojos
ArrayList
创建 View
s。 LightRecyclerView
对数据库中的内容一无所知,只知道 mLightPojos
中的项目。当您从数据库中删除或更新某些内容时,它不会自动从mLightPojos
中删除或更新。你必须在通知之前处理好这个问题。
谢谢我的问题解决了@MikeM。
@MikeM。我遇到了同样的问题。感谢您的解决方案。
【参考方案1】:
从 Fragment 或 Activity(无论您使用什么)更新您的数据模型调用 adapter.notifydatasetChanged()
。然后recyclerView会更新。
【讨论】:
【参考方案2】:更新数据模型后请调用以下方法。
adapter.notifyItemRemoved(position);
这将更新适配器关于从列表中删除一个元素。
【讨论】:
【参考方案3】:是的,我发现我必须在单击时显式更改 ArrayList。除了数据库更新。
在将值更新到 SQLite 调用适配器方法以在内部更新值之后。每次更新数据时,您无需获取 SQLite 数据。
【讨论】:
以上是关于当sqlite android片段中的数据更改或删除时如何刷新recyclerview?的主要内容,如果未能解决你的问题,请参考以下文章
如何从android studio中的recycler视图将数据添加到SQLite数据库中
Jellybean 中的 Android SQLite 数据库事务/锁定更改