首先我们来看下效果(简单来说,有点像,qq或微信的聊天的列表)
(java版)
首先新建项目,为ListViewText,然后创建好avd,在activity_main写上代码
java
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/lv"/></LinearLayout>
然后再新建list_item.xml ,
java
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/tv"android:textSize="30sp"android:textColor="@color/black"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
activity_main文件是整个屏幕,而list_item.xml是屏幕的的小列表(不知道我解释清楚了吗)
然后在java里面新建Bean类与MyAdapter类
Bean里面两方法
java
packagecom.example.listviewtext; publicclassBean { Stringname; publicStringgetName() { returnname; } publicvoidsetName(Stringname) { this.name=name; } }
MyAdapter里面
java
packagecom.example.listviewtext; importandroid.content.Context; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.TextView; importjava.util.List; publicclassMyAdapterextendsBaseAdapter { privateList<Bean>data;//创建私有的Bean类的dataprivateContextcontext; publicMyAdapter(List<Bean>data, Contextcontext) { this.data=data; this.context=context; } publicintgetCount() { returndata.size();//获取data的长度 } publicObjectgetItem(inti) { returnnull; } publiclonggetItemId(inti) {//获取idreturni; } publicViewgetView(inti, Viewview, ViewGroupviewGroup) { if (view==null){//防止view不停的新建,view=LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false); } TextViewtextView=view.findViewById(R.id.tv); textView.setText(data.get(i).getName());//系统会去R文件里面找type类型的值匹配String值returnview; } }
然后MainActivity里面代码
java
packagecom.example.listviewtext; importandroidx.appcompat.app.AppCompatActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.ListView; importjava.util.ArrayList; importjava.util.List; publicclassMainActivityextendsAppCompatActivity { privateList<Bean>data=newArrayList<>();//ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (inti=1; i<=100; i++) { Beanbean=newBean(); bean.setName("第"+i+"个"); data.add(bean);//通过add把数据加入到data里面 } ListViewlistView=findViewById(R.id.lv);//获取id,就是上面讲的屏幕里面的列表的idlistView.setAdapter(newMyAdapter(data,this));//Adapter作用将各种数据以合适的形式显示在View中给用户看 } }
然后点击运行,看效果
如果不想通过for传值的话,还可以用new 的方法,一个一个创建
java
packagecom.example.listviewtext; importandroidx.appcompat.app.AppCompatActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.ListView; importjava.util.ArrayList; importjava.util.List; publicclassMainActivityextendsAppCompatActivity { privateList<Bean>data=newArrayList<>(); protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Beanbean=newBean(); bean.setName("a"); data.add(bean); Beanbean1=newBean(); bean1.setName("b"); data.add(bean1); ListViewlistView=findViewById(R.id.lv); listView.setAdapter(newMyAdapter(data,this)); } }
看效果
(kotlin版)
首先新建项目,为ListViewText,然后创建好avd,在activity_main写上代码
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>
然后MainActivity里面代码
packagecom.example.listview2importandroidx.appcompat.app.AppCompatActivityimportandroid.os.Bundleimportandroid.widget.ArrayAdapterimportkotlinx.android.synthetic.main.activity_main.*classMainActivity : AppCompatActivity() { privatevaldata=listOf("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15") overridefunonCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) valadapter=ArrayAdapter(this,android.R.layout.simple_list_item_1,data) listView.adapter=adapter } }
效果如下: