美文网首页
【六】ListView

【六】ListView

作者: 晴天ccc | 来源:发表于2020-07-13 13:49 被阅读0次

    acitvity_main

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingDefaultResource">
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listviewId">
        </ListView>
    </RelativeLayout>
    

    MainActivity

    public class MainActivity extends AppCompatActivity {
        //数据源
        private  static final String[] commonFunList = new String[]{
                "ListView1",
                "ListView2",
                "ListView3",
                "ListView4",
                "ListView5"
        };
        private ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 初始化
            listView = (ListView)findViewById(R.id.listviewId);
            //setAdapter:布局
            listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, commonFunList));
            //setOnItemClickListener:监听点击了哪一条list
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Dialog alertDiaLog = new AlertDialog.Builder(MainActivity.this).
                            setMessage("您点击了第" + i + "个item").create();
                    alertDiaLog.show();
                }
            });
        }
    }
    

    相关文章

      网友评论

          本文标题:【六】ListView

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