zoukankan      html  css  js  c++  java
  • 安卓list

    package com.example.myapplication;

    import androidx.appcompat.app.AppCompatActivity;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

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

    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);
            initFruits(); // 初始化水果数据
            FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
            ListView listView = (ListView) findViewById(R.id.list_view);
            listView.setAdapter(adapter);
        }

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

  • 相关阅读:
    vim——打开多个文件、同时显示多个文件、在文件之间切换(转)
    内核任务调度与数据结构
    chrome浏览器iframe兼容性问题,隐藏起来再显示滚动条消失?
    九、迭代器、生成器、函数递归调用与二分法
    八、函数、闭包、装饰器
    七、函数
    六、字符编码、文件
    五、列表、元组、字典、集合详解
    四、字符串及其内置方法
    三、内存管理、数据类型、基本运算符、流程控制
  • 原文地址:https://www.cnblogs.com/wishings/p/14064167.html
Copyright © 2011-2022 走看看