Use of global variables and local variables in Android

Use of global variables and local variables in Android

As the name implies, global variables are variables that can be called in the entire class or in multiple functions. They are also called external variables. Local variables are variables that can be accessed in a specific procedure or function. It is easy to declare a variable, but when it comes to using it, it is not as simple as imagined. As for me, I often define global variables for use, but because of this, I define global variables. It also takes a lot of detours.

When using Adapter, it is usually used with listView, because a listView basically has a layout of listView items. The following scenario is: there is an ImageView in each item. When I click on an item, I need to change the background color of the ImageView of the item or change it to another background image. At this time, a situation may occur that you clearly click on the first item, but you will find that the image of the third or second item has also changed. This is because you define a global variable. The code part is as follows:

  1. public   class Adapter extends BaseAdapter {
  2. private ImageView img;
  3.   
  4. public View getView( int position, View convertView, ViewGroup parent) {
  5. convertView = mInflater.inflate(R.layout.group_listview_item, null );
  6. img = (ImageView) convertView.findViewById(R.id.logo);
  7.   
  8. return convertView;
  9.   
  10. } }

In the above part, ImageView is a global variable. At this time, we need to define ImageView as a local variable.

  1. <pre> public   class Adapter extends BaseAdapter {
  2.   
  3. public View getView( int position, View convertView, ViewGroup parent) {
  4. convertView = mInflater.inflate(R.layout.group_listview_item, null );
  5. ImageView img = (ImageView) convertView.findViewById(R.id.logo);
  6.   
  7. return convertView;
  8.   
  9. } }</pre>

This means that each Item has an ImageView. Another case is when you are making a shopping cart, you can click the plus and minus icons to change the number of items in the shopping cart. When you define the number num, you must also define it as a local variable. It would be better if you can use ViewHolder.

Original link: http://blogs.zmit.cn/6371.html

<<:  After the failure of Fire Phone, Amazon's hardware team Lab126 began to turmoil

>>:  Summary of AndroidStudio shortcut keys

Recommend

How to write a good title? All the tricks for a “100,000+” title!

As we all know, the number of readers of public a...

Why does the central bank want to take over WeChat and Alipay?

[[236022]] [[236023]] [[236024]] Why does the cen...

Tik Tok operation and promotion plan, super detailed!

Recently, Douyin released the "2020 Spring F...

How to operate the remaining traffic of the content platform?

Content platforms have surplus traffic every day....

How to get higher conversions on Zhihu? Zhihu oCPC delivery skills~

It is becoming increasingly difficult to operate ...

Analysis of competitive products of community group buying products

As social e-commerce has developed to the present...

E-commerce traffic, operation and activity system!

E-commerce + private domain = social e-commerce. ...

How fast can humans accelerate particles?

The only martial arts in the world that cannot be...

Shanghai, why does we need Lingang?

Looking down on Shanghai from the sky, people can...

How many people will be unemployed after the epidemic?

Since I started work yesterday, my circle of frie...

Sun Jian's Family Education Course

"Sun Jian's Premium Family Education Cour...