<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.运行效果图</span>
2.
<1>SLVActivity
package cn.edu.bzu.seniorlv;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class SLVActivity extends Activity {
private List<Animal> animalList = new ArrayList<Animal>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slv);
initAnimal();// 初始化动物数据
AnimalAdapter adapter = new AnimalAdapter(SLVActivity.this, R.layout.animal_item, animalList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>parent,View view,int position,long id){
Animal animal = animalList.get(position);
Toast.makeText(SLVActivity.this,animal.getName(),Toast.LENGTH_SHORT).show();
}
});
}
private void initAnimal() {
// TODO Auto-generated method stub
Animal bear = new Animal("Bear", R.drawable.bear);
animalList.add(bear);
Animal bird = new Animal("Bird", R.drawable.bir2);
animalList.add(bird);
Animal cat = new Animal("Cat", R.drawable.cat);
animalList.add(cat);
Animal dog = new Animal("Dog", R.drawable.dog);
animalList.add(dog);
Animal donkey = new Animal("Donkey", R.drawable.don);
animalList.add(donkey);
Animal duck = new Animal("Duck", R.drawable.duc);
animalList.add(duck);
Animal elephant = new Animal("Elephant", R.drawable.ele);
animalList.add(elephant);
Animal frog = new Animal("Frog", R.drawable.frog);
animalList.add(frog);
Animal hen = new Animal("Hen", R.drawable.hen2);
animalList.add(hen);
Animal monkey = new Animal("Monkey", R.drawable.mon);
animalList.add(monkey);
}
}
<2>Animal.java
package cn.edu.bzu.seniorlv;
public class Animal {
private String name;
private int imageId;
public Animal(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
<3>AnimalAdapter.java
package cn.edu.bzu.seniorlv;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AnimalAdapter extends ArrayAdapter<Animal> {
private int resourceId;
public AnimalAdapter(Context context,int textViewResourceId,List<Animal>objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Animal animal = getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.animalImage = (ImageView)view.findViewById(R.id.animal_image);
viewHolder.animalName = (TextView)view.findViewById(R.id.animal_name);
view.setTag(viewHolder);//将ViewHolder存储在View中
}
else{
view = convertView;
viewHolder = (ViewHolder)view.getTag();//重新获取ViewHolder
}
viewHolder.animalImage.setImageResource(animal.getImageId());
viewHolder.animalName.setText(animal.getName());
return view;
}
class ViewHolder{
ImageView animalImage;
TextView animalName;
}
}
<4>activity_slv.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
<5>animal_item.xml
<?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/animal_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/animal_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
3.存在的问题:
<1>导的包是一个bundle,有的根本用不上,导致出现了很多的warning.
<2>不知道为什么,我的eclipse的虚拟机无法启动,手机进行USB调试以后,也检测不到设备,所以我的程序根本无法运行。
<3>图片大小未修改,刚开始无法使用,将图片修改至几十kb以后,运行的界面就比较美观了。