ListView Learning Notes in android
1. Use Array Adapter to provide data to ListView
Layout file
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
Code
public class MainActivity extends Activity {
private String [] data = {"Apple","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
}
}
2. Customize Array Adapter
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
JavaBean objects
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
Adapter code
public class FruitAdapter extends ArrayAdapter<Fruit>{
private int resourceID;
public FruitAdapter(Context context,int textViewResourceId,
List<Fruit> objects) {
super(context,textViewResourceId, objects);
resourceID = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//According to the incoming coordinates, the corresponding items are obtained, each of which is a fruit object.
Fruit fruit = getItem(position);
//Generate View Objects Based on Your Customized Layout Files
View view = LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, null);
//Find the corresponding control in your own custom layout
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
//Set up corresponding resources on this item (fruit)
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
//Return to the system, let the system call
return view;
}
Main activity
public class MainActivity extends Activity {
// private String [] data =
// {"Apple","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA","Banana","NOKIA"};
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, data);
FruitAdapter adapter = new FruitAdapter(this,R.layout.fruit_item,fruitList);
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
fruitList.add(mango);
}
}
3. Optimizing customization
Thought: This getView method does not produce a single call, so the program will start with how many items on the screen will produce, how many items will call the getView method, when the code is not optimized, every time the screen slides, it will hide one item, so the system will re-view, so sliding many times will produce many V view. Iew object. So the second parameter of getView is the reference to the item that slides to hide, so we can use it directly.
Modify FruitAdapter's getView method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// According to the incoming coordinates, the corresponding items are obtained, each of which is a fruit object.
Fruit fruit = getItem(position);
View view = null;
if (convertView != null) {
//Direct multiplexing
view = convertView;
} else {
// Generate View Objects Based on Your Customized Layout Files
view = LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, null);
}
// Find the corresponding control in your own custom layout
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
// Set up corresponding resources on this item (fruit)
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
// Return to the system, let the system call
return view;
}
Summary: getView, which generates a new View object every time it is called, converView is to take the hidden View directly and test it with Log!
4. Re-optimization
Thought: findViewById will be called every time we get the View object (which has been optimized to get the View object), but each item of us corresponds to a fruit and is an immutable corresponding relationship, so we only need to set up a resource file once, not every call to getView, so there is a problem? That's how to create a constant relationship between the View object and the fruit.
We define a bean object and use it to save the control (no need to find VB). When the view object is generated for the first time, it shows that this item has not been generated yet, so we need find to get the control. After that, the reference to the control is saved in the bean, and then the bean object is saved with view.setTag(obj), so that it is related. When the view object is covertView, the view has been generated, so the carrier bean object inside can be used directly.
This is the way I write it. If I don't know it, I'll take a look at the code myself.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// According to the incoming coordinates, the corresponding items are obtained, each of which is a fruit object.
Fruit fruit = getItem(position);
View view = null;
//Create your own bean s
ViewHolder viewHolder = null;
if (convertView != null) {
//Direct multiplexing
view = convertView;
//In this item, you have your own bean s, which you can extract directly.
viewHolder = (ViewHolder)view.getTag();
} else {
// Generate View Objects Based on Your Customized Layout Files
view = LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, null);
//There is no bean of its own in this item. Create a new one.
viewHolder = new ViewHolder();
//Save it in your own bean s
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
//Save it in each View, and each View corresponds to a fruit.
view.setTag(viewHolder);
}
Log.d("test", "call..." + view.hashCode());
//Unified Setting up Resources
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
// Return to the system, let the system call
return view;
}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
Set the click event for listview
Modify the main activity code
Add click events to listview
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});