How to Sync whole List instead of only visible records

假如想象 提交于 2019-12-11 19:55:33

问题


Trying to Sync complete List, by doing tap on button, but instead of syncing whole list code only syncing records visible in a list, my mean when i do scroll down to list, it is not syncing records.

For example, I have 300 records in a list, but only 10 records are visible to camera screen for rest need to scroll, so my program only Syncing those 10 records, not whole list, why ?

    ImageButton buttonSync = (ImageButton) findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

             for(int i=0; i<lstView.getChildCount(); i++)
             {
                 startUpload(i);
             }          
        }
    });

回答1:


The getChildCount() method returns only the total number of children currently visible in the listview. You will have to get the data to be synced from the data-source (provided to the listview adapter). That is the correct way of doing it.

However, if you have to sync an item in the listview and update that particular view after syncing it, you can leverage the adapter's notifyDataSetChanged. You can have a flag that is checked to see if that particular record of the listview is to be updated or not.

// An array of flags, as many as the number of records in the listview
// such that, flag[0] is set to true to indicate that the first item in the
// listview needs to call startUpload()
private SparseBooleanArray flags = new SparseBooleanArray();

// At onClick, set all the flags to indicate that some data needs to be synced
ImageButton buttonSync = (ImageButton) findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

             for(int position=0; position<listView.getAdapter().getCount(); position++)
             {
                 flags.put(position, true);
             }

             // Calling this would ensure a call to getView() on every
             // visible child of the listview. That is where we will check if
             // the data is to be synced and displayed or not
             ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
        }
    });

@Override
// In getView of the listview's adapter
public View getView(int position, View convertView, ViewGroup parent) {

    // If this item is to be synced
    if(flags.get(position)) {
        startUpload();

        // Mark as synced
        flags.put(position, false);
    }

    // Rest of the method that draws the view....
}


来源:https://stackoverflow.com/questions/21495199/how-to-sync-whole-list-instead-of-only-visible-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!