A summary of global variables and local variables used in Android _android

Source: Internet
Author: User

Global variables, as the name suggests, are variables that are called in the entire class or in multiple functions. Also called an external variable. A local variable is a variable that can be accessed in a particular procedure or function. Declaring a variable is easy, but when it comes to use, it's not as simple as it might seem. As for me, we often define the use of global variables, but that's why we define global variables. Also spared a lot of bend.

When using the adapter adapter, the adapter is usually used with ListView because a ListView basically has a ListView item layout, and the following scenario is: There is a imageview in each item, When I click on a particular item, I need to change the background color for the ImageView or change to a different background image, one of the situations that may occur is the first one you clearly point, you will find that the third or second picture also changed. That's because you're defining a global variable. The code section is as follows:

Copy Code code as follows:

public class Adapter extends Baseadapter {
Private ImageView img;

Public View GetView (int position, View Convertview, ViewGroup parent) {
Convertview = Minflater.inflate (r.layout.group_listview_item,null);
img = (imageview) Convertview.findviewbyid (R.id.logo);

return convertview;

} }


In the above section, ImageView is a global variable. And this time, we need to define ImageView as a local variable,
Copy Code code as follows:

public class Adapter extends Baseadapter {

Public View GetView (int position, View Convertview, ViewGroup parent) {
Convertview = Minflater.inflate (r.layout.group_listview_item,null);
ImageView img = (imageview) Convertview.findviewbyid (R.id.logo);

return convertview;

} }


This time means the ImageView in each item. There is also a situation in the shopping cart, you can click on the Add and subtract icon to change the number of items in the shopping cart. When you define number num, you must also define it as a local variable. It would be better if you could use Viewholder.

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.