Using ArrayAdapter add data to the ListView
<?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 android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="1dp" android:headerDividersEnabled="false"/> <!-- 设置使用绿色的分隔条 --> <ListView android:id="@+id/list2" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#0f0" android:dividerHeight="1dp" android:headerDividersEnabled="false"/> </LinearLayout>
We create an array, and add it to ArrayAdapter, then linked up with the ListView.
List1 = the findViewById Val < the ListView > (R.id.list1) // define an array val arr1 = arrayOf ( "Monkey", "pig", "cow devil") // array packaging the ArrayAdapter Val Adapter1 the ArrayAdapter = (the this , R.layout.array_item, of arr1) // Adapter set to the ListView list1.adapter Adapter1 = Val = List2 the findViewById < the ListView > (R.id.list2) // define an array val arr2 = arrayOf ( "Java" , "Hibernate "," the Spring "," Android ") // array packaging ArrayAdapter Val adapter2 = ArrayAdapter (the this, R.layout.checked_item,arr2) // set Adapter for the ListView list2.adapter = adapter2
Note here that the adapter is to create three arguments:
- contex
- textViewResourceId, this is the layout file.
- Array or list
Layout file looks like this:
Details like this:
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:checkMark="@drawable/ok" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2"/>
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24dp" android:padding="5dp" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2"/>