Java 添加新数据时更新回收器视图
当我向列表中添加新数据时,更新recylcer视图时遇到问题。该列表仅在我重新启动应用程序时更新。 我尝试在我的“listAktinovsti”函数中使用Adapter.notifyItemChanged(array.size()-1),但不起作用,我还尝试将notifyItemChanged添加到我的alterdialog on click函数中,但也不起作用。这是到目前为止我的代码。如果需要,我将发布适配器类 这是我的片段类。Java 添加新数据时更新回收器视图,java,android,Java,Android,当我向列表中添加新数据时,更新recylcer视图时遇到问题。该列表仅在我重新启动应用程序时更新。 我尝试在我的“listAktinovsti”函数中使用Adapter.notifyItemChanged(array.size()-1),但不起作用,我还尝试将notifyItemChanged添加到我的alterdialog on click函数中,但也不起作用。这是到目前为止我的代码。如果需要,我将发布适配器类 这是我的片段类。 public class TodoFragment extend
public class TodoFragment extends Fragment {
private OnListFragmentInteractionListener mListener;
AppDatabase db;
ToDoRecyclerViewAdapter mmAdapter;
RecyclerView recyclerView;
public TodoFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
final FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
openDB();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(getActivity());
View popupView = li.inflate(R.layout.popup_layout, null);
final EditText editText = popupView.findViewById(R.id.userInput);
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
adb.setView(popupView);
adb.setCancelable(false)
.setPositiveButton("Dodaj", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String naziv = editText.getText().toString();
Aktivnost_ ak = new Aktivnost_(naziv, null,0,null);
dodajAktivnost(ak);
}
})
.setNegativeButton("Prekliči", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
Toast.makeText(getContext(), "Preklical sem", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = adb.create();
alertDialog.setCancelable(true);
alertDialog.show();
}
});
recyclerView = (RecyclerView) view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
mmAdapter = new ToDoRecyclerViewAdapter(listAktivnosti(),mListener);
recyclerView.setAdapter(mmAdapter);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnListFragmentInteractionListener {
void onListFragmentInteraction(Aktivnost_ item);
}
public void dodajAktivnost(Aktivnost_ ak) {
boolean dodaj = db.addRow(ak);
if(dodaj) {
Toast.makeText(getContext(), "dodano v bazo", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "nope", Toast.LENGTH_SHORT).show();
}
}
public void openDB() {
db = new AppDatabase(getContext());
db.open();
}
public List<Aktivnost_> listAktivnosti() {
openDB();
ArrayList<Aktivnost_> array = new ArrayList<>();
Aktivnost_ ak;
Cursor cursor = db.getAllRows();
while(cursor.moveToNext()) {
ak = new Aktivnost_();
ak.setId_(cursor.getLong(cursor.getColumnIndex("_id")));
ak.setNaziv(cursor.getString(cursor.getColumnIndex("naziv")));
ak.setDatum(cursor.getString(cursor.getColumnIndex("datum")));
ak.setFk_projekt(cursor.getInt(cursor.getColumnIndex("fk_projekt")));
ak.setUdeleženci(cursor.getString(cursor.getColumnIndex("udelezenci")));
array.add(ak);
}
return array;
}
}
公共类TodoFragment扩展了片段{
私有OnListFragmentInteractionListener-MLListener;
数据库数据库;
ToDoreRecycleServiceAdapter-MmaAdapter;
回收视图回收视图;
公众集会{
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u todo\u list,container,false);
最终浮动ActionButton浮动ActionButton=view.findViewById(R.id.fab);
openDB();
floatingActionButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
LayoutInflater li=LayoutInflater.from(getActivity());
视图popupView=li.充气(R.layout.popup\u布局,空);
final EditText EditText=popupView.findviewbyd(R.id.userInput);
AlertDialog.Builder adb=新建AlertDialog.Builder(getContext());
adb.setView(popupView);
adb.setCancelable(假)
.setPositiveButton(“Dodaj”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
字符串naziv=editText.getText().toString();
Aktivnost_uuak=新的Aktivnost_uuz(naziv,null,0,null);
多达扎克提夫诺斯特(ak);
}
})
.setNegativeButton(“Prekliči”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
dialogInterface.cancel();
Toast.makeText(getContext(),“Preklical sem”,Toast.LENGTH_LONG.show();
}
});
AlertDialog AlertDialog=adb.create();
alertDialog.setCancelable(真);
alertDialog.show();
}
});
recyclerView=(recyclerView)view.findViewById(R.id.list);
setLayoutManager(新的LinearLayoutManager(getActivity());
addItemDecoration(新的DividerItemDecoration(getContext(),LinearLayoutManager.VERTICAL));
mmAdapter=newtodorecycleServiceAdapter(listAktivnosti(),mListener);
recyclerView.setAdapter(mmAdapter);
返回视图;
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnListFragmentInteractionListener的上下文实例){
mListener=(OnListFragmentInteractionListener)上下文;
}否则{
抛出新的RuntimeException(context.toString()
+“必须实现OnListFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
公共接口OnListFragmentInteractionListener{
无效在线碎片交互(Aktivnost_uuu项目);
}
公共无效dodajAktivnost(Aktivnost_uuak){
布尔值dodaj=db.addRow(ak);
if(dodaj){
Toast.makeText(getContext(),“dodano v bazo”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(getContext(),“nope”,Toast.LENGTH_SHORT.show();
}
}
public void openDB(){
db=新的AppDatabase(getContext());
db.open();
}
公共列表listAktivnosti(){
openDB();
ArrayList数组=新的ArrayList();
Aktivnost_uuak;
Cursor=db.getAllRows();
while(cursor.moveToNext()){
ak=新的Aktivnost_u2;();
ak.setId(cursor.getLong(cursor.getColumnIndex(“\u id”));
setNaziv(cursor.getString(cursor.getColumnIndex(“naziv”));
ak.setDatum(cursor.getString(cursor.getColumnIndex(“datum”));
ak.setFk_项目(cursor.getInt(cursor.getColumnIndex(“fk_项目”));
ak.setUdeleženci(cursor.getString(cursor.getColumnIndex(“udelezenci”));
数组.add(ak);
}
返回数组;
}
}
更改数据后,请在更改数据后立即添加此代码:
yourAdapterName.notifyDataSetChanged()
这应该适合您。在您的代码中,我不清楚您到底在哪里添加了新项目。如果在正极按钮的onClick()中:
ArrayList array=new ArrayList()代码>来自listAktivnosti
并将其放在公共类TodoFragment扩展片段{
数组
并调用notifyDataSetChanged()当您添加新的数据调用notifyDataSetChanged()或notifyItemInserted(int position)时,我会在代码“Aktivnost_uUak=NEW Aktivnost(naziv,null,0,null);dodajAktivnost(ak)的这一部分添加新数据;但我已经尝试从那里调用notifyDataSetChanged函数,但它不起作用。我也尝试过从我的函数调用它”我再次向sqllite添加数据的“dodajAktivnost”不起作用