ListView简单的实例讲解

实现简单的文字展现
效果展现:

实现步骤:

1.修改MainActivity中的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.listviewtest.MainActivity">

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

    </ListView>

</LinearLayout>

2.修改MainActivity中的代码,绑定控件,设置适配器

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    //定义一个字符串数组
    private String[] player = {"梅西","德布劳内","萨内","斯特林","热苏斯","席尔瓦","飞鸟","孔帕尼","沃克","爱德森","门第","斯通斯","布冯","皮克","贝尔","罗纳尔多"};

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

        listView = findViewById(R.id.main_list_view);
        //创建适配器
        /**
         * @param context The current context. 当前的上下文
         * @param resource The resource ID for a layout file containing a TextView to use when
         *                 instantiating views.   listView子项布局的id
         * @param objects The objects to represent in the ListView.  适配的数据
         */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,player);
        //绑定适配器
        listView.setAdapter(adapter);
    }
}
over

想要看更复杂的功能可以看这篇博客:
我写的另一篇博客

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

intoSunshine

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

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

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

打赏作者

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

抵扣说明:

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

余额充值