Main5Activity 代碼如下:
public class Main5Activity extends AppCompatActivity {
private List<Fruit> fruitList =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
initFruits();
FruitAdapter adapter =new FruitAdapter(Main5Activity.this,R.layout.fruit_item,fruitList);
ListView listView=(ListView)findViewById(R.id.list_View);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fruit fruit =fruitList.get(position);
Toast.makeText(Main5Activity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
}
private void initFruits(){
for (int i=0;i<2;i++){
Fruit apple = new Fruit("Apple",R.drawable.apple);
fruitList.add(apple);
Fruit banbana=new Fruit("Banbana",R.drawable.banbana);
fruitList.add(banbana);
}
}
}
activity_main5.xml 代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lnjr.ll.Main5Activity">
<ListView
android:id="@+id/list_View"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
FruitAdapter.java代碼如下:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent){
Fruit fruit =getItem(position);
View view;
ViewHolder viewHolder;
if (convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolder=new ViewHolder();
viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
viewHolder.fruitName=(TextView)view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
}else {
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
}
Fruit.java 代碼如下:
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 int getImageId(){
return imageId;
}
}
fruit_item.xml 代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
>
</TextView>
</LinearLayout>