一、ListView基本使用
- 在XML布局文件中文件中,添加控件代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- listview -->
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
并为控件设置idandroid:id="@+id/listview"
- 在Activity文件中对控件进行绑定初始化
private ListView listview;
private void initView() {
listview = (ListView) findViewById(R.id.listview);
}
- 接下来我们为ListView设置适配器,这里我们先使用ArrayAdapter
public class ListViewActivity extends AppCompatActivity {
private ListView listview;
private ArrayAdapter<String> mArrayAdapter;
private List<String> list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);