简体   繁体   中英

Update recycler view when new data is added

Im having trouble updating my recylcer view when i add new data to the list. The list only updates when i restart my app. I've tried using Adapter.notifyItemChanged(array.size()-1) in my "listAktinovsti" function but didnt work, i have also tried adding notifyitemchanged to my alterdialog on click function and that also didnt work. Here's my code so far. i will post my adapter class if its needed.

This is my fragment class.

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;
    }

}

After you changed your data, add this code right after when you changed your data: yourAdapterName.notifyDataSetChanged()

This should work for you.

In your code it is not clear to me where exactly you do add the new item. If it is in the onClick() of the positive button:

  1. remove this declaration ArrayList<Aktivnost_> array = new ArrayList<>(); from listAktivnosti and put it after the header of the public class TodoFragment extends Fragment {
  2. after adding the item to the db, add the item to array and call notifyDataSetChanged()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM