Android Studio 列表Listview的简单用法

⭐⭐⭐⭐⭐⭐适配器设置方法⭐⭐⭐⭐⭐⭐⭐
ArrayAdapter设置参数依次为 上下文,布局,数据源

myAdapter = new ArrayAdapter(xxxActivity.this,android.R.layout.simple_expandable_list_item_1,xxx);

制作一个列表以逐行显示文本:

一、布局:

Android Studio 列表Listview的简单用法_第1张图片


二、Java代码:

package com.example.unit6_1listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
private ListView mylistview;
private ArrayAdapter myAdapter;
private String[] books = { "奥特曼","阿童木","哪吒","钢铁侠","蜘蛛侠","美国队长"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mylistview = findViewById(R.id.mylistview);
        myAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_expandable_list_item_1,books);
        mylistview.setAdapter(myAdapter);
    }
}


三、结果展示:

Android Studio 列表Listview的简单用法_第2张图片

你可能感兴趣的:(android,studio,android,android-studio)