关闭

ListView的应用

标签: listview
99人阅读 评论(0) 收藏 举报

关于ListView,最基本的应该就是写适配器了。关于适配器,我这里使用的是ArrayAdapter,可以根据自己的需求使用适配器

虽说现在有了RecycleView,但是简单的用ListView还是比较顺手

下面直接贴代码介绍一下ListView的应用


适配器的代码如下

package com.example.listviewtexst;

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 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) {
		Fruit fruit = getItem(position);//获取当前项的Fruit实例
		
		LayoutInflater li = LayoutInflater.from(getContext());
		View view = li.inflate(resourceId, null);
//		View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
		ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
		TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
		fruitImage.setImageResource(fruit.getImageId());
		fruitName.setText(fruit.getName());
		return view;
	}
	
}

封装类Fruit

package com.example.listviewtexst;

public class Fruit {
	private String name;
	private int imageId;
	
	public Fruit (String name,int imageId) {
		this.setName(name);
		this.setImageId(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;
	}
	
	
}


Activity的代码:

package com.example.listviewtexst;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;

public class MainActivity extends Activity {
	
	private List<Fruit> fruitList = new ArrayList<Fruit>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intiFruits();//初始化水果数据
		FruitAdapter adapter = new FruitAdapter(this, R.layout.fruit_item, fruitList);
		ListView listView = (ListView) findViewById(R.id.list_view);
		listView.setAdapter(adapter);
	}

	private void intiFruits() {
		Fruit apple = new Fruit("Apple", R.drawable.apple);
		fruitList.add(apple);
		Fruit banana = new Fruit("Banana", R.drawable.banana);
		fruitList.add(banana);
		Fruit orange = new Fruit("Orange", R.drawable.orange);
		fruitList.add(orange);
		Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon);
		fruitList.add(watermelon);
		Fruit pear = new Fruit("Pear", R.drawable.pear);
		fruitList.add(pear);
		Fruit grape = new Fruit("Grape", R.drawable.grape);
		fruitList.add(grape);
		Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple);
		fruitList.add(pineapple);
		Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry);
		fruitList.add(strawberry);
		Fruit cherry = new Fruit("Cherry", R.drawable.cherry);
		fruitList.add(cherry);
		Fruit mango = new Fruit("Mango", R.drawable.mango);
		fruitList.add(mango);
	}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16867次
    • 积分:413
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条