-1

I want to implement custom progressview on base adapter for showing background download progress on list items, every second I check download progress and update my model, then trigger notifydataset to reflect the progress to UI. Since it couses so may UI refresh operations, I cannot get click events properly. What should I do to overcome this?

  @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {

                startJob();
            }
        }
    });
    t.start();
    return START_STICKY;
}

start job method

 public void startJob() {
    BusStation.getMainThreadBus().post("deneme");

    DownloadManager mDownloadManager = new DownloadManager(getContentResolver(),
            getPackageName());
    mDownloadManager.setAccessAllDownloads(true);

    DownloadManager.Query myDownloadQuery = new DownloadManager.Query();

    Cursor cursor = mDownloadManager.query(myDownloadQuery);

    ArrayList<Integer> statuses = new ArrayList<>();

    try {
        DergilikApplication application = (DergilikApplication) getApplicationContext();
        HashMap<String, DownloadInfo> downlodInfoHashMap = application.getDownlodInfoHashMap();
        while (cursor.moveToNext()) {
            int mIdColumnId = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);

            int mIdStatus = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);

            int mTotalBytesColumnId = cursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
            int mCurrentBytesColumnId = cursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
            int mDescrColumnId = cursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION);
            int uriId = cursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_URI);
            int localUriId = cursor
                    .getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI);
            long totalBytes = cursor.getLong(mTotalBytesColumnId);
            String desc = cursor.getString(mDescrColumnId);


            long currentBytes = cursor.getLong(mCurrentBytesColumnId);
            long downloadId = cursor.getLong(mIdColumnId);
            int status = cursor.getInt(mIdStatus);
            String localUri = cursor.getString(localUriId);
            String uri = cursor.getString(uriId);
            if (application.getProfile() != null && desc.contains(application.getProfile().getLoginNumber())) {
                statuses.add(status);


                DownloadInfo downlodInfo = new DownloadInfo();

                downlodInfo.setCurrentProgress(currentBytes);
                downlodInfo.setTotalProgress(totalBytes);
                downlodInfo.setDescription(desc);
                downlodInfo.setStatus(status);
                downlodInfo.setDownloadId(downloadId);
                downlodInfo.setUri(uri);
                downlodInfo.setLocalUri(localUri);
                downlodInfoHashMap.put(desc, downlodInfo);
            }


        }
        application.setDownlodInfoHashMap(downlodInfoHashMap);
    } finally {
        cursor.close();
    }

    int counter = 0;
    for (Integer status : statuses) {
        if (!(status == DownloadManager.STATUS_FAILED || status == DownloadManager.STATUS_SUCCESSFUL)) {
            counter++;
        }

    }
    if (counter == statuses.size()) {
    }

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


}

i push event with otto library and notfy data set changed

 @Subscribe
public void getMessage(String s) {


    try {
        dergilerAdapter.notifyDataSetChanged();
    } catch (Exception e) {
        e.printStackTrace();
    }
    //tvDownloaded.setText("% " + (int) (percentage * 100));

}
hugerde
  • 919
  • 1
  • 12
  • 27

1 Answers1

1

When you call notifyDataSetChanged(), all items of the list view are deleted and recreated. That means you cannot click properly on an item. In addition, you are spamming the UI thread with useless calls.

Prefer use a call to notifyItemChanged(position) to only refresh the row itself where the progress bar is. That way, the click events will be handled properly.

doubotis
  • 219
  • 2
  • 10
  • I am using GridView and BaseAdapter , notifyItemChanged(position) method , RecyclerView method , I will try to use RecyclerView thanks for your answer – hugerde Aug 26 '16 at 08:41