如何同步整个列表,而不是唯一可见的记录 [英] How to Sync whole List instead of only visible records

查看:251
本文介绍了如何同步整个列表,而不是唯一可见的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试同步完整列表后,通过按钮做水龙头,但而不是同步整个列表code仅在列表同步记录可见,我的意思是,当我做向下滚动到列表中,它不同步的记录。

例如,我有 300条记录在列表中,但只有10记录到摄像机屏幕上可见休息需要滚动,所以我的程序只同步那些10记录,而不是整个名单,为什么?

 的ImageButton buttonSync =(的ImageButton)findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根             的for(int i = 0; I< lstView.getChildCount();我++)
             {
                 startUpload(ⅰ);
             }
        }
    });


解决方案

的getChildCount()方法返回目前唯一在ListView可见孩子的总数。您将获得的数据从数据源(提供给ListView适配器)进行同步。这是做它的正确途径。

然而,如果您有同步在ListView的项目,同步后更新该特定视图,您可以利用适配器的<一个href=\"http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29\"相对=nofollow> notifyDataSetChanged 。可以有被检查的标志,以查看是否列表视图的该特定的记录是要被更新或不

  //一个标志阵列,多达记录在ListView数
//这样,标志[0]设置为true,表示在第一项
//列表视图需要调用startUpload()
私人SparseBooleanArray标志=新SparseBooleanArray();//在的onClick,设置所有的标志,表明需要一些数据同步
的ImageButton buttonSync =(的ImageButton)findViewById(R.id.sync_btn);
    buttonSync.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){             对于(INT位置= 0;&位置LT; listView.getAdapter()getCount将();位置++)
             {
                 flags.put(位置,真正的);
             }             //调用此将确保每到getView()的调用
             //列表视图的可见子。这就是我们将检查
             //中的数据是要同步并显示或不
             ((BaseAdapter)listView.getAdapter())notifyDataSetChanged();
        }
    });@覆盖
// ListView中的适配器的getView
公共查看getView(INT位置,查看convertView,父母的ViewGroup){    //如果此项目是同步
    如果(flags.get(位置)){
        startUpload();        //标记为已同步
        flags.put(位置,FALSE);
    }    绘制视图的方法//休息....
}

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

解决方案

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

这篇关于如何同步整个列表,而不是唯一可见的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆