listView 列表的使用方法

listView 列表 对于初学者来说有点难理解,可以慢慢消化,忘记了可以来看看。

mainactivity.java

package com.example.lianxi_xioala;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    
    
    private List<Bean> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        for (int i = 0; i < 20; i++) {
    
    
            Bean bean = new Bean();
            bean.setName("享学" + i);
            Log.d("bean:",""+bean);
            data.add(bean);
        }
        ListView listView = findViewById(R.id.lv);
        listView.setAdapter(new MyAdapter(data,this));
    }
   }

Bean.java

package com.example.lianxi_xioala;

public class Bean {
    
    
    String name;
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
    this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Bean{" +
                "name='" + name + '\'' +
                '}';
    }
}


listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

list.item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="match_parent"/>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_44584479/article/details/115210837

相关文章