在安卓的数据列表显示的时候,通常会用到ListView,所以列出下学习时候遇到的
1,用系统自定义的ListView
首先在xml文件中建立listview
<ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView>然后在MainAvtivity中建立数据,此时的数据一般都是提前准备好的, 一般是网上下载的或者从数据库中提取的
private String[] data = { "Apple" , "Banana" , "Orange" , "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState); requestWindowFeature(Window. FEATURE_NO_TITLE); setContentView(R.layout. activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1 , data ); ListView listview = (ListView) findViewById(R.id.list_view ); listview.setAdapter( adapter); }
<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_gravity="center" android:layout_marginLeft="10dp" />
建立一个类,继承适配器类
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId ; public FruitAdapter(Context context, int resource, List<Fruit> objects) { super( context, resource, objects); // TODO Auto-generated constructor stub resourceId= resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /*return super.getView(position, convertView, parent);*/ //获取当前的fruit实例 Fruit fruit= getItem( position); View view= LayoutInflater.from(getContext()).inflate(resourceId , null); ImageView fruitImage= (ImageView) view.findViewById(R.id. fruit_image); TextView fruitNmae=(TextView) view.findViewById(R.id. fruit_name); fruitImage.setImageResource( fruit.getImageId()); fruitNmae.setText( fruit.getName()); return view; }
public class MainActivity extends Activity { /*private String[] data = { "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };*/ private List<Fruit> fruitList = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState); /*requestWindowFeature(Window.FEATURE_NO_TITLE);*/ setContentView(R.layout. activity_main ); /*ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, data); ListView listview = (ListView) findViewById(R.id.list_view); listview.setAdapter(adapter);*/ //初始化水果 initFruit(); FruitAdapter adapter= new FruitAdapter(MainActivity. this, R.layout.fruit_item , fruitList ); ListView listView=(ListView) findViewById(R.id.list_view ); listView.setAdapter( adapter); } private void initFruit() { // TODO Auto-generated method stub 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); }主要是listview完成初始化,然后产生一个自定义的适配器进行装配数据,传给ListView
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; public FruitAdapter(Context context, int resource, List<Fruit> objects) { super(context, resource, objects); // TODO Auto-generated constructor stub resourceId=resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /*return super.getView(position, convertView, parent);*/ //获取当前的fruit实例 Fruit fruit= getItem(position); /* * convertView主要是将之前加载好的布局进行缓存 */ View view; ViewHolder viewHolder; if(convertView==null) { view= LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder=new ViewHolder(); viewHolder.fruitImage=(ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName=(TextView) view.findViewById(R.id.fruit_name); } else { view =convertView; viewHolder=(ViewHolder) view.getTag(); } /* ImageView fruitImage= (ImageView) view.findViewById(R.id.fruit_image); TextView fruitNmae=(TextView) view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitNmae.setText(fruit.getName());*/ viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImage; TextView fruitName; } }
增加点击事件
可以在每次点击列表项的每一项的时候,响应事件,可以在MainActivity最后增加响应
//设置列表响应事件 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Fruit fruit=fruitList.get(position); Toast.makeText(MainActivity.this,"您点击了"+fruit.getName(), Toast.LENGTH_SHORT).show(); } });