Can’t scroll down on list after runnable is added

  android-studio, runnable

I am having a problem with my list view as after I added a runnable I am not able to scroll down, as every time I try to scroll down it keep on bringing me back up to the top of the page. Below is the code I used in the fragment class

public class DeleteFragment extends Fragment {

    private ListView listViewCrime;
    DatabaseHelper DB_CRIME;
    private ListAdapterDelete adapter;
    private Handler handler;
    private Runnable runnable;

    private DeleteViewModel deleteViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        deleteViewModel =
                new ViewModelProvider(this).get(DeleteViewModel.class);
        View root = inflater.inflate(R.layout.fragment_delete, container, false);


        listViewCrime = root.findViewById(R.id.listViewCrime);

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this, 1000);
                try {
                    loadData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, 1 * 1000);

        final TextView textView = root.findViewById(R.id.text_display);
        deleteViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        return root;

    }

    private void loadData() {
        DB_CRIME = new DatabaseHelper(getActivity());
        List<CrimesDelete> crime = DB_CRIME.getCrimesDelete();
        adapter = new ListAdapterDelete(getActivity(), crime);
        if (crime != null) {
            listViewCrime.setAdapter(adapter);

        }
    }
} 

Source: Android Studio Questions

LEAVE A COMMENT