'Using SimpleCursorAdapter and MatrixCursor with threads to retrieve and view data faster in android studio

So i basically found a way to retrieve my data from a room database to a listview ( i cant find the post again to add the link) but i've come to a point where the data is a lot and it takes a lot of time to load. Can anyone help me with a multithread implementation?

private void setUpOrRefreshListView() {

        new Thread(new Runnable() {

            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        //loadCircle.setVisibility(View.VISIBLE);
                        csr = getCursor();
                        if (sca == null) {
                            sca = new SimpleCursorAdapter(
                                    getApplicationContext(),
                                    R.layout.costum_listview_layout,csr,
                                    new String[]{"latitude","longitude","altitude","accuracy","velocity","bearing","time"},
                                    new int[]{R.id.customListView_latitude,R.id.customListView_longitude,R.id.customListView_altitude,R.id.customListView_accuracy,R.id.customListView_velocity,R.id.customListView_bearing,R.id.customListView_time},
                                    0
                            );
                            listView.setAdapter(sca);
            /* handle Long Click of an Item in the ListView
                in this case just Toast info
            */
                            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                                @SuppressLint("Range")
                                @Override
                                public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Toast.makeText(
                                            view.getContext(),
                                            "You long-clicked gps data with id = "+ l
                                                    +" and coordinates are "+ csr.getString(csr.getColumnIndex("latitude"))
                                                    + ", " + csr.getString(csr.getColumnIndex("longitude"))+" recorded "+ csr.getString(csr.getColumnIndex("time"))
                                            ,
                                            Toast.LENGTH_LONG
                                    ).show();
                                    return true;
                                }
                            });
                        } else {
                            sca.swapCursor(csr);
                        }
                    }
                });
                handler.post(new Runnable() {
                    public void run() {
                        //loadCircle.setVisibility(View.INVISIBLE);

                    }
                });

            }
        }).start();


    }

    private Cursor getCursor() {

        MatrixCursor mxcsr = new MatrixCursor(new String[]{
                BaseColumns._ID,
                "latitude",
                "longitude",
                "altitude",
                "accuracy",
                "velocity",
                "bearing",
                "time"},
                0
        );
        for (MyLocation p: locationRepository.getLocations()) {
            mxcsr.addRow(new Object[]{p.getId(),"x: "+p.getLatitude(),"y: "+p.getLongitude(),"alt: "+new DecimalFormat("##.###").format(p.getAltitude()),"acc: "+new DecimalFormat("##.###").format(p.getAccuracy()),"vel: "+new DecimalFormat("##.###").format(p.getVelocity()),"bear: "+new DecimalFormat("##.###").format(p.getBearing()),"time: "+fixTime(p.getTime())});
        }
        return mxcsr;
    }

Ive added only one thread so not everything will happen on the main thread but it still wont really help if i have to load more than 10000 data from sqlite. I call the method on the onCreate.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source