Explanation about the Android:listview getchildat empty

Source: Internet
Author: User

Today I encountered a very strange problem, I set a ListView has 10 item, when the number of my list.getcount for the For Loop, when the 6th item, the resulting view object is null, which makes me very puzzled, After a query, understand the problem is how to share with you:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3A/74/wKioL1O7t5iTMp4kAAC_DbxzQ8E536.jpg "title=" Indicator briefs. png "alt=" wkiol1o7t5itmp4kaac_dbxzq8e536.jpg "/>

The listview portion of the Red box section of the slice. This actually involves the memory processing problem of the Android ListView control: When we load the ListView, if the ListView has more data, it is not fully displayed, but is all the data loaded? The answer is no, the number of ListView loading is the current screen display of more than four, respectively, the top and bottom of each buffer two, this is the problem. When I have a ListView with 10 item, but my current layout shows up to 4, so when I get to the seventh one, I get an empty item. The solution is as follows:

public class Measureitemlistener implements onitemclicklistener{


@Override

public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,

Long Arg3) {

FIRSTLOCATION=ARG2;

dataldialog= New Loaddialogdao (Getactivity (), "Data Loading ...");

Dataldialog.show ();

for (int i=0;i<measure_type_list.getcount (); i++) {

int firvisible=measure_type_list.getfirstvisibleposition (); \ \ Gets the index of the first visible item

View V=measure_type_list.getchildat (i-firvisible);

if (v!=null) {

if (arg2 = = i) {

V.setbackgroundcolor (R.color.lightblue);

} else {

V.setbackgroundresource (r.drawable.list_item_selector);

}

}

}

}

};

The solution is to add the red character part, get the index of the first visible item on the screen, and subtract this value from the current child's index to get the current object.


This article from "Mo Panic, raise straight forward" blog, please be sure to keep this source http://kunyali.blog.51cto.com/4890065/1435989

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.