Android Studio初学笔记-Day15-ListView

ListView

Listview是个能装载多个view的列表元件,在资料的展示时很常被使用。Listview还有个很重要的观念,就是使用Adapter配适器,其实在上一篇的Spinner中也有使用到Adapter,不过没有仔细讲解到Adapter的功能,其实Adapter是个在配适资料和版面很常见的工具,我会在下面的例子中透过程序码来接续解释它的功能。

介面程序码

<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="140dp"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="20dp"
        android:text="备忘录"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="-16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity程序码

public class MainActivity extends AppCompatActivity {
    private ListView lv1;
    String data[]={"牛奶","高丽菜","猪肉","水果","零食"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv1=(ListView)findViewById(R.id.lv1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                                       (this,android.R.layout.simple_list_item_1,data);
        lv1.setAdapter(adapter);
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,data[position],Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 这里简单设计一个字串阵列,作为放置资料的地方。
  • 用ArrayAdapter来配适资料,这里可以把Adapter当作一个是中介连接的角色,负责连接资料和Listview的部分,所以先将字串(data)绑上Adapter,这里面的android.R.layout.simple_list_item_1是android提供Listview的layout,这里也可以依自己的需求改成自己设计的layout。
  • 绑上字串後就可以把adapter交给Listview达到连接的功能。也可以加入按钮事件,设计当清单内容被按下的话所需要做的事情。

成果

https://ithelp.ithome.com.tw/upload/images/20210907/20139136ZiCS1QHPCQ.png
今天Listview就讲到这边,谢谢大家~/images/emoticon/emoticon41.gif


<<:  Day15 PHP函数介绍

>>:  DAY16支持向量机演算法(续五)

密码学基础篇

很多人听到密码学一定都吓得逼逼挫, 感觉密码学就很难很多数学很复杂, 不用担心,其实,我也是跟各位一...

[铁人赛 Day14] 来读 Hooks FAQ 文件-lifecycle methods 如何对照到 Hooks?

lifecycle methods 如何对照到 Hooks? constructor:Functio...

面对自己阴暗面的修练

不管是Scrum Master或者扮演Product Owner,抑或是传统的专案管理者,在整个专案...

Proxmox VE 虚拟机管理操作 (二)

虚拟机的建置与操作已经来到基本使用的程度了,对於各种应用修改与尝试後可能伴随虚拟机被搞砸的风险。 ...

[Day 28] 实作 Multi-Channel Notifications

铁人赛已逐渐进入尾声,前面二十多天,我们一步步扩充加强 Ktor 功能,也整合了 ORM, Redi...