Android Problem Summary II (update UI issues)

Source: Internet
Author: User

Today the test video information client, with the new UI is the LogCat console error: Only the original thread that created a view hierarchy can touch it views. Search on the Internet only to find that the original Android related view and control is not thread-safe, we have to do a separate processing. So here again the handler is used to operate the class. It was here that I realized that I had already used this way to work with the new UI before. I didn't expect it to be forgotten here. Record here the province later forget again:

The code is as follows:

The use of handler and the new UI

Final Handler Handler = new Handler () {

public void Handlemessage (Message msg) {

Switch (msg.what) {

Case 0:

Listview.setadapter (adapter);

break;

Default:

Toast.maketext (Getapplicationcontext (), R.string.error, 1)

. Show ();

break;

}

}

};

//Use line loads to load data

New Thread (new Runnable () {

@Override

public void run () {

try {

list<video> videos = Videoservice.getall ();//need to modify the HTTP request path to your native computer

list

for (Video Video:videos) {

System.out.println ("---------------------------------------= = =" +video);

hashmap<string, object> item = new hashmap<string, object> ();

item.put ("id", Video.getid ());

item.put ("name", Video.getname ());

item.put ("Time", Getresources (). getString (R.string.timelength)

+ video.gettimelength () + getresources (). getString (R.string.min));

Data.add (item);

}

adapter = new Simpleadapter (Getapplicationcontext (), data, R.layout.items,

New string[]{"name", "Time"}, new Int[]{r.id.name, r.id.time});

handler.sendemptymessage (0);

} catch (Exception e) {

e.printstacktrace ();

}

}

}). Start ();

Finally in terms of handler this class:

Official Description:A Handler allows you to send and processMessageand Runnable objects associated with a thread ' s MessageQueue. Each Handler instance are associated with a single thread and that thread ' s message queue. When you create a new Handler, it's bound to the thread/message queue of the thread that's creating it--from that PO int on, it'll deliver messages and runnables to that message queue and execute them as they come out of the message Queu E.

Characteristics:

handler can distribute the Message object and the Runnable object into the main thread, and each handler instance is bound to create his line approached (typically in the main thread).
it has two functions: (1): Schedule a message or runnable to execute somewhere in a main thread, (2) Schedule an action to execute on a different thread
some ways to distribute messages in handler
Post (Runnable)
postattime (Runnable,long)
postdelayed (Runnable long)
sendemptymessage (int)
sendMessage (Message)
sendmessageattime (Message,long)
sendmessagedelayed (Message,long)
The Post class method above allows you to arrange a runnable object into the main thread queue,
The SendMessage class method allows you to schedule a message object with data to queue and wait for updates.

Update the UI instance with handler: (This is someone else's writing here to keep a reference)

Steps:

1. Create the Handler object (this is created in the main thread for easy updating of the UI).

2, constructs the Runnable object, updates the interface in the runnable.

3. Update the UI by post,runnable the object to the UI thread in the child thread's Run method.



Android Problem Summary II (update UI issues)

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.