标签:
1.添加图片文件
在res下drawable-hdpi文件里添加所需的图片资源;
例:guide1.jpg,guide2.jpg,guide3.jpg,guide4.jpg,guide5.jpg,guide6.jpg,guide7.jpg,guide8.jpg.
2.MainActivity.java文件
1 package com.example.listview; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.view.Menu; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class MainActivity extends Activity { 13 //private String[] data = { "Apple", "Banana", "Orange", "Watermelon", 14 //"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" }; 15 private List<Fruit> fruitList = new ArrayList<Fruit>(); 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 initFruits(); // 初始化水果数据 22 FruitAdapter adapter = new FruitAdapter(MainActivity.this, 23 R.layout.fruit_item, fruitList); 24 ListView listView = (ListView) findViewById(R.id.listView1); 25 listView.setAdapter(adapter); 26 27 /*ArrayAdapter<String> adapter = new ArrayAdapter<String>( 28 MainActivity.this, android.R.layout.simple_list_item_1, data); 29 ListView listView = (ListView) findViewById(R.id.listView1); 30 listView.setAdapter(adapter);*/ 31 } 32 private void initFruits() { 33 Fruit apple = new Fruit("Apple", R.drawable.guide1); 34 fruitList.add(apple); 35 Fruit banana = new Fruit("Banana", R.drawable.guide2); 36 fruitList.add(banana); 37 Fruit orange = new Fruit("Orange", R.drawable.guide3); 38 fruitList.add(orange); 39 Fruit watermelon = new Fruit("Watermelon", R.drawable.guide4); 40 fruitList.add(watermelon); 41 Fruit pear = new Fruit("Pear", R.drawable.guide5); 42 fruitList.add(pear); 43 Fruit grape = new Fruit("Grape", R.drawable.guide6); 44 fruitList.add(grape); 45 Fruit pineapple = new Fruit("Pineapple", R.drawable.guide7); 46 fruitList.add(pineapple); 47 Fruit strawberry = new Fruit("Strawberry", R.drawable.guide8); 48 fruitList.add(strawberry); 49 } 50 51 52 @Override 53 public boolean onCreateOptionsMenu(Menu menu) { 54 // Inflate the menu; this adds items to the action bar if it is present. 55 getMenuInflater().inflate(R.menu.main, menu); 56 return true; 57 } 58 59 }
3.Fruit.java文件
1 package com.example.listview; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.view.Menu; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class MainActivity extends Activity { 13 //private String[] data = { "Apple", "Banana", "Orange", "Watermelon", 14 //"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" }; 15 private List<Fruit> fruitList = new ArrayList<Fruit>(); 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 initFruits(); // 初始化水果数据 22 FruitAdapter adapter = new FruitAdapter(MainActivity.this, 23 R.layout.fruit_item, fruitList); 24 ListView listView = (ListView) findViewById(R.id.listView1); 25 listView.setAdapter(adapter); 26 27 /*ArrayAdapter<String> adapter = new ArrayAdapter<String>( 28 MainActivity.this, android.R.layout.simple_list_item_1, data); 29 ListView listView = (ListView) findViewById(R.id.listView1); 30 listView.setAdapter(adapter);*/ 31 } 32 private void initFruits() { 33 Fruit apple = new Fruit("Apple", R.drawable.guide1); 34 fruitList.add(apple); 35 Fruit banana = new Fruit("Banana", R.drawable.guide2); 36 fruitList.add(banana); 37 Fruit orange = new Fruit("Orange", R.drawable.guide3); 38 fruitList.add(orange); 39 Fruit watermelon = new Fruit("Watermelon", R.drawable.guide4); 40 fruitList.add(watermelon); 41 Fruit pear = new Fruit("Pear", R.drawable.guide5); 42 fruitList.add(pear); 43 Fruit grape = new Fruit("Grape", R.drawable.guide6); 44 fruitList.add(grape); 45 Fruit pineapple = new Fruit("Pineapple", R.drawable.guide7); 46 fruitList.add(pineapple); 47 Fruit strawberry = new Fruit("Strawberry", R.drawable.guide8); 48 fruitList.add(strawberry); 49 } 50 51 52 @Override 53 public boolean onCreateOptionsMenu(Menu menu) { 54 // Inflate the menu; this adds items to the action bar if it is present. 55 getMenuInflater().inflate(R.menu.main, menu); 56 return true; 57 } 58 59 }
4.FruitAdapter.java文件
1 package com.example.listview; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ArrayAdapter; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 13 public class FruitAdapter extends ArrayAdapter<Fruit> { 14 private int resourceId; 15 16 public FruitAdapter(Context context, int textViewResourceId,List<Fruit> objects) { 17 super(context, textViewResourceId,objects); 18 resourceId = textViewResourceId; 19 // TODO 自动生成的构造函数存根 20 } 21 public View getView(int position, View convertView, ViewGroup parent) { 22 Fruit fruit = getItem(position); // 获取当前项的Fruit实例 23 View view = LayoutInflater.from(getContext()).inflate(resourceId, null); 24 ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); 25 TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); 26 fruitImage.setImageResource(fruit.GetimageId()); 27 fruitName.setText(fruit.GetName()); 28 return view; 29 } 30 31 }
5.AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.listview" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="18" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.example.listview.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
6.activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <ListView 9 android:id="@+id/listView1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" > 12 </ListView> 13 14 </LinearLayout>
7.fruit_item.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" > 4 <ImageView 5 android:id="@+id/fruit_image" 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" /> 8 <TextView 9 android:id="@+id/fruit_name" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_gravity="center" 13 android:layout_marginLeft="10dip" /> 14 15 </LinearLayout>
8.结果预览图
标签:
原文地址:http://www.cnblogs.com/chance88/p/4803193.html