美文网首页
ListView的简单用法

ListView的简单用法

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-10-26 19:30 被阅读3次

//布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hzx.mylistview.MainActivity">

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

</LinearLayout>


//Acitivity

public class MainActivity extends AppCompatActivity {

    private  String[] data = {"Apple","Banana","Orange",
    "Watermelo","Pear","Grape","Pineapple",
    "Strawberry","Cherry","Mango",
    "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);

    }
}

相关文章

网友评论

      本文标题:ListView的简单用法

      本文链接:https://www.haomeiwen.com/subject/rbvltqtx.html