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.