Tag Archives: Local refresh

Local refresh of listview and GridView

As we all know, listview and GridView refresh the interface by calling adapter.notifyDataSetChanged () refresh the interface. But this method has its disadvantages. It refreshes all the data in the interface, no matter whether the data has changed or not. If listview loads a lot of data (e.g. 100 entries)

When refreshing, it will cause a lot of system overhead. How to refresh only one like QQ space personal dynamic

Main principles:
refresh an item in listview
1. Get the current index position and data of the item to be refreshed
2. Reset the acquired data
3. Put the reset data in the original position of the data set in the adapter (refresh a piece of data in the original data set according to the position)
4. Get the view of the sub item to be refreshed in listview
5. Get new data from the updated data set and update the data in viwe (operate in handler to refresh the interface)

The functions are as follows, with detailed comments in the code:

public class MainActivity extends Activity  
{  

    private ArrayList<MyListItem> list = null;  
    private ListView              lv;  
    private MyListAdapter         adapter;  

    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        intitData();  
        lv = (ListView) findViewById(R.id.listView1);  
        adapter = new MyListAdapter(list, getApplicationContext());  
        adapter.setListView(lv);  
        lv.setAdapter(adapter);  

        lv.setOnItemClickListener(new OnItemClickListener()  
        {  

            @Override  
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)  
            {  
                // Get the data of the clicked item in the listview   
                MyListItem item = (MyListItem) parent.getItemAtPosition(position);  
                Log.i("eee", item.getData() + " == " + item.getPosition());  
                // Update data  
                item.setData("update item " + position);  
                // Update Interface  
                adapter.updateItemData(item);  
            }  
        });  

    }  

    /** 
     * Initialization data 
     */  
    private void intitData()  
    {  
        list = new ArrayList<MyListItem>();  
        for (int i = 0; i < 20; i++)  
        {  
            MyListItem item = new MyListItem();  
            item.setData("item " + i);  
            item.setPosition(i);  
            list.add(item);  
        }  
    }  

    /** 
     * Customize item data type 
     */  
    class MyListItem  
    {  
        /** 
         * data id 
         */  
        private int    dataId;  
        /** 
         * datas 
         */  
        private String data;  

        public int getPosition()  
        {  
            return dataId;  
        }  

        public void setPosition(int position)  
        {  
            this.dataId = position;  
        }  

        public String getData()  
        {  
            return data;  
        }  

        public void setData(String data)  
        {  
            this.data = data;  
        }  

    }  
}  
activity to call, the function operation is mainly encapsulated in the adapter as follows.

public class MyListAdapter extends BaseAdapter  
{  

    /** 
     * Data sets in listview 
     */  
    private ArrayList<MyListItem> mDataList;  

    private Context               mContext;  
    private ListView              mListView;  

    public MyListAdapter(ArrayList<MyListItem> list, Context cont)  
    {  
        this.mDataList = list;  
        this.mContext = cont;  
    }  

    /** 
     * Set the listview object 
     *  
     * @param lisv 
     */  
    public void setListView(ListView lisv)  
    {  
        this.mListView = lisv;  
    }  

    /** 
     * update listview Single data 
     *  
     * @param item New data object 
     */  
    public void updateItemData(MyListItem item)  
    {  
        Message msg = Message.obtain();  
        int ids = -1;  
        // Compare data to get the position of the corresponding data in the list  
        for (int i = 0; i < mDataList.size(); i++)  
        {  
            if (mDataList.get(i).getPosition() == item.getPosition())  
            {  
                ids = i;  
            }  
        }  
        msg.arg1 = ids;  
        // Update the data in the corresponding position of mDataList  
        mDataList.set(ids, item);  
        // handle refreshes the interface  
        han.sendMessage(msg);  
    }  

    @SuppressLint("HandlerLeak")  
    private Handler han = new Handler()  
                        {  
                            public void handleMessage(android.os.Message msg)  
                            {  
                                updateItem(msg.arg1);  
                            };  
                        };  

    /** 
     * Refresh the specified item 
     *  
     * @param index item's position in listview 
     */  
    private void updateItem(int index)  
    {  
        if (mListView == null)  
        {  
            return;  
        }  

        // Get the currently visible position of the item  
        int visiblePosition = mListView.getFirstVisiblePosition();  
        // If you add headerview, firstview is hearderview  
        // All indexes + 1 to get the first view  
        // View view = listview.getChildAt(index - visiblePosition + 1);  
        // Get the clicked view  
        View view = mListView.getChildAt(index - visiblePosition);  
        TextView txt = (TextView) view.findViewById(R.id.textView1);  
        // get mDataList.set(ids, item); the updated data  
        MyListItem data = (MyListItem) getItem(index);  
        // Reset the interface to display data  
        txt.setText(data.getData());  
    }  

    @Override  
    public int getCount()  
    {  
        // TODO Auto-generated method stub  
        return mDataList.size();  
    }  

    @Override  
    public Object getItem(int position)  
    {  
        // TODO Auto-generated method stub  
        return mDataList.get(position);  
    }  

    @Override  
    public long getItemId(int position)  
    {  
        // TODO Auto-generated method stub  
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent)  
    {  
        // TODO Auto-generated method stub  
        if (convertView == null)  
        {  
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);  
        }  
        TextView txt = (TextView) convertView.findViewById(R.id.textView1);  
        txt.setText(mDataList.get(position).getData());  
        return convertView;  
    }  

}  

No matter how far and hard the road ahead is, as long as the direction is right, no matter how rugged, it is closer to happiness than standing in the same place!