添加新数据时更新回收站视图

时间:2018-06-04 15:39:04

标签: java android

我在向列表添加新数据时无法更新我的重新指定视图。该列表仅在我重新启动应用程序时更新。 我尝试在我的" listAktinovsti"中使用Adapter.notifyItemChanged(array.size() - 1)。功能,但没有工作,我也尝试将clickitemchanged添加到我的alterdialog点击功能,这也没有工作。到目前为止,这是我的代码。如果需要,我会发布我的适配器类。

这是我的片段类。

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

}

2 个答案:

答案 0 :(得分:0)

更改数据后,请在更改数据后立即添加此代码: yourAdapterName.notifyDataSetChanged()

这应该适合你。

答案 1 :(得分:0)

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()
相关问题