1
已經有2天了,我仍然不知道爲什麼當我在sqlite
數據庫中插入數據時,我的RecyclerView
不能立即更新。如何在Sqlite數據庫中添加新數據時更新回收站視圖
我一直在嘗試了許多方法,例如nofityDataSetChanged
和swapAdapter
但是我的RecyclerView
將不會更新,更新它,我需要去到另一個活動之前的數據已經被添加到RecyclerView
。
所以誰能告訴我我在哪裏做錯了?
繼承人我查詢
public void AddData(){
reg_pet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items=new ArrayList<>();
final int position=0;
adapter=new reg_Adapter(getActivity(),items);
boolean insterdata = myDB.insertDataToPetRegister(pet_name.getText().toString(),
spin_type.getSelectedItem().toString(), spin_breed.getSelectedItem().toString());
if (insterdata = true) {
adapter.notifyItemInserted(position);
adapter.notifyDataSetChanged();
pet_name.setText("");
Toast.makeText(getActivity(), "Your Pet has been Registered", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Your Pet has not been Registerd", Toast.LENGTH_LONG).show();
}
}
});
}
我的適配器
public class reg_Adapter extends RecyclerView.Adapter<reg_ViewHolder> {
Activity activity;
Register_pet_database register_pet_database;
List<db_getItem> items;
db_getItem adapter;
DBAdapter dbAdapter;
public reg_Adapter(Activity activity, List<db_getItem> items) {
this.activity = activity;
this.items = items;
}
public reg_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_pet_recycler, parent, false);
return new reg_ViewHolder(view);
}
@Override
public void onBindViewHolder(reg_ViewHolder holder, final int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
holder.btndel.setTag(items.get(position).getPet_name());
holder.btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
Integer deleted =register_pet_database.deleteData(v.getTag().toString());
if (deleted > 0) {
items.remove(position);
notifyItemRemoved(position);
Toast.makeText(v.getContext(), "Pet Has been Remove", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(v.getContext(), "Pet not Removed", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return items.size();
}
}
我在我的片段代碼
代碼public class pet_tab extends Fragment {
RecyclerView recyclerView;
Register_pet_database register_pet_database;
ArrayList<db_getItem> arrayList;
reg_Adapter reg_adapter;
db_getItem item;
ImageButton btndel;
TextView pet_name;
Cursor c;
public pet_tab() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_pet_tab, container, false);
recyclerView=(RecyclerView)view.findViewById(R.id.reg_pet_recycler);
btndel=(ImageButton)view.findViewById(R.id.btndel);
loadDb();
return view;
}
public void loadDb(){
register_pet_database=new Register_pet_database(getActivity());
DBAdapter db=new DBAdapter(getActivity());
db.openDB();
arrayList=new ArrayList<>();
c=register_pet_database.quearyData("select * from Pet_Registered");
final int position=0;
try {
if(c!=null){
if(c.moveToFirst()){
do {
db_getItem item = new db_getItem();
item.setPet_name(c.getString(1));
item.setPet_type(c.getString(2));
item.setPet_breed(c.getString(3));
arrayList.add(item);
}while(c.moveToNext());
}
}
}catch (SQLException e){
e.printStackTrace();
}
reg_Adapter adapter=new reg_Adapter(getActivity(),arrayList);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);;
recyclerView.swapAdapter(adapter,false);
}
}