Android Studio 实验四:手机通讯录(ListView的应用)

该博客介绍了如何在Android中使用ListView来展示水果列表。通过创建FruitAdapter适配器,结合Fruit类和fruit_item.xml布局文件,实现了ListView中每个条目显示水果名称和图片的功能。在MainActivity中初始化水果数据并设置Adapter,最终成功展示了包含多个Fruit实例的列表。

目录

activity_main.xml

FruitAdapter

Fruit类

fruit_item.xml

Activity

运行截图

项目文件


activity_main.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">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

FruitAdapter

(虽然是通讯录,但是因为书上是拿水果举的例子,所以文件的命名也就照搬了)在包内创建一个Java类并命名为FruitAdapter,输入代码如下:

public class FruitAdapter extends ArrayAdapter<Fruit> {

    private int resouceId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resouceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position);

        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resouceId,parent,false);
        } else {
            view = convertView;
        }

        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类

在包里创建Java类并命名为Fruit,代码如下:

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

 创建layout文件并命名为fruit_item,代码如下:

<?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="wrap_content">

    <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_vertical"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

Activity

public class MainActivity extends AppCompatActivity {

    private List<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruits();//初始化水果数据
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,
                R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

    }


//对图片进行加载,图片放在mian/res/drawable里面
    private void initFruits() {
        for(int i=0; i < 3; i++) {
            Fruit apple = new Fruit("周杰伦",R.drawable.image04);
            fruitList.add(apple);
            Fruit banana = new Fruit("哆啦",R.drawable.image02);
            fruitList.add(banana);
            Fruit orange = new Fruit("天皇",R.drawable.image03);
            fruitList.add(orange);
        }
    }
}

 

运行截图

  

项目文件

AndroidStudio实验四:手机通讯录(ListView的应用)-Android文档类资源-优快云下载AndroidStudio实验四:手机通讯录(ListView的应用)更多下载资源、学习资料请访问优快云下载频道.https://download.youkuaiyun.com/download/nazonomaster/85219538

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nazonomaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值