效果图
1.main.xml文件里编写布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mainListView1"/>
</LinearLayout>
2.MainActivity.class类文件里编写代码
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.util.*;
public class MainActivity extends Activity
{
private ListView lv;
private ArrayAdapter<String> adp;
private ArrayList<String> array;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=findViewById(R.id.mainListView1);
array=new ArrayList<String>();
for(int i=0;i<18;i++){
array.add("测试"+i);
}
adp=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
lv.setAdapter(adp);
//更新数据
adp.notifyDataSetChanged();
}
}