Android custom Cursor has a small problem.

Source: Internet
Author: User

Android custom Cursor has a small problem.

In many cases, Android applications use SQliter to store a large amount of data. Using sqlliter cannot help but deal with cursor. flexible use of cursor saves a lot of trouble. For example, you can combine your data in a virtual table (data set) and return it to the user in the form of cursor through the Provider. You can also specify the number of records returned by the cursor each time, to reduce the load pressure on the large amount of UI data. (If you only encapsulate the data returned in cursor mode, MetrixCursor can meet this requirement. Its publicvoid addRow (Object [] columnValues) method can help you maintain the added Object array data ).

The specific implementation code is as follows:

Final Map <String, String> data = new HashMap <String, String> (); data. put ("wifi", "on"); Cursor cursor = new AbstractCursor () {private String [] names; /*** number of records ** @ return */@ Override public int getCount () {return data. size ()> 0? 1: 0;}/*** get the name of each column. For SharedPreferences, each key * @ return */@ Override public String [] getColumnNames () {if (names = null) {names = new String [0]; if (! Data. isEmpty () {names = new String [data. size ()]; Set <String> keySet = data. keySet (); names = (String []) keySet. toArray (new String [0]) ;}} return names ;}@ Override public String getString (int column) {String key = names [column]; return (String) data. get (key) ;}@ Override public short getShort (int column) {return 0 ;}@ Override public int getInt (int column) {return 0 ;} @ Override public long getLong (int column) {return 0 ;}@ Override public float getFloat (int column) {return 0 ;}@ Override public double getDouble (int column) {return 0 ;}@ Override public boolean isNull (int column) {return false ;}};
The first line of code is worth noting. It was written in this way:

if(names == null) {     names = new String[0];     if (!data.isEmpty()) {           names = new String[data.size()];           Set<String> keySet = data.keySet();           int i = 0;           for (String key : keySet) {           names[i++] = key;           }     } }
However, the method toArray () provided by Set to convert to Array is changed
names = (String[])keySet.toArray();
However, the result is incorrect. When a breakpoint tracking is triggered, data is found in the cursor, but an error occurs if the result is obtained.

For Set, it only knows that the Object is saved internally, so by default, toArray can only return an array of objects composed of these objects.
However, the author of the program may be more aware of the more specific types of its internal elements. Therefore, the Set class provides another overloaded version of toArray, you can specify an array type that is more specific than Object [] by passing an array instance of the array type you want, it doesn't matter how long it is (so we often use a zero length, after all, it is OK to bring the type into), so the toArray will be based on the type you want internally, to construct an array. The array constructed in this way is of course safely converted back to the actual type by the caller.

Reference: http://bbs.csdn.net/topics/100149308

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.