Android Custom cursor encounters a small problem

Source: Internet
Author: User

Androidapplications that store large amounts of data in many casesSqliter,UseSqlliterinevitably to andCursordeal with, flexible useCursorwill save a lot of things. If you combine your data in a virtual table (a data collection),ProviderinCursorreturn to the user, you can also specifyCursorhow many records are returned per fetch to reduceUiload pressure with a large amount of data. (If you just encapsulate the data toCursorway Back,MetrixcursorThis requirement can be completed and itsPublicvoid AddRow (object[]Columnvalues)method can help you maintain the object array data you added.)。

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;                /** * Record number * @return */@Override public int GetCount () { return data.size () > 0?            1:0; }/** * Gets the name of each column, for sharedpreferences is every key * @return */@Over Ride public string[] Getcolumnnames () {if (names = = null) {names = new Strin                    G[0];                        if (!data.isempty ()) {names = new string[data.size ()];                        set<string> KeySet = Data.keyset (); <span style= "color: #ff0000;" >names = (string[]) Keyset.toarray (new string[0]);</span>}} RET            Urn 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 red part is the code to note, which begins with this:

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;}}                        }
But set has a method of toarray () that turns into an array, so it's changed to
<span style= "color: #ff0000;" >names = (string[]) Keyset.toarray ();</span>
But the result was wrong, and the cursor was found to have data after the break, but it would be wrong to get the flowers.

For set, it only knows that it has an object inside it, so by default, ToArray can only return an object array consisting of these objects.
However, the author of the program may be more aware of the more specific types of its internal elements, so the set class provides another overloaded version of ToArray, allowing the user to specify a more specific array type than object[] by passing an array instance of the array type that the user wants to enter, How long it doesn't matter (so we often use a 0-length, after all, take the type into the OK), so, toarray inside will follow the type you want to construct an array out. The constructed array is, of course, very safe for the caller to convert back to that actual type.

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

Android Custom cursor encounters a small problem

Related Article

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.