美文网首页
ListView普通列表控件的使用

ListView普通列表控件的使用

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

    //布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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.ble_scan_broadcast.MainActivity">
    
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>
    
    </RelativeLayout>
    
    

    //activity

    public class MainActivity extends AppCompatActivity {
    
        private ListView listView;
        private ArrayAdapter<String> adapter;
    
        private List<String> data = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            data = MyDataSource.getDataSource();
            //adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
            adapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_single_choice,data);
    
            listView = (ListView)findViewById(R.id.listview);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setAdapter(adapter);
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
    
                    Toast.makeText(MainActivity.this,"click me",1).show();
    
                    return false;
                }
            });
        }
    }
    
    
    
    image.png

    相关文章

      网友评论

          本文标题:ListView普通列表控件的使用

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