android#ListView的简单用法

新建项目,并修改项目生产的主文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</LinearLayout>

编写对应activity代码

public class MainActivity extends Activity {

     private String[] data = { "Apple", "Banana", "Orange",     "Watermelon",
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1, data);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
}
上一篇:javascript – Vue.JS – 微前端方法


下一篇:【Android基础】listview控件的使用(1)------最简单的listview的使用