ArrayAdapter简单实现 Listview

来源:互联网 发布:个人信用数据库包含 编辑:程序博客网 时间:2024/06/13 02:29

ArrayAdapter简单实现 Listview

你现在受的苦,未来都会照亮你的路,告诉所有正在路上奋斗的人,即使路途遥远,即使梦想遥遥无期,但只要坚持下去,总会得到最好的结果……

package com.crazyit.ui.adapterview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class ArrayAdapterActivity extends AppCompatActivity {    private ListView lv1,lv2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_array_adapter);        //初始化控件        lv1 = (ListView) findViewById(R.id.lv1);        lv2 = (ListView) findViewById(R.id.lv2);        //定义一个数组        String[] str1 = new String[]{"魏国","蜀国","吴国"};        //将数组包装成ArrayAdapter        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplication(),R.layout.array_item,str1);        //为listView设置Adapter        lv1.setAdapter(adapter1);        //定义一个数组        String[] str2 = new String[]{"JAVA","Android","HTML","PHP"};        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,R.layout.checked_item,str2);     //为listView设置Adapter        lv2.setAdapter(adapter2);    }}

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:orientation="vertical"    tools:context="com.crazyit.ui.adapterview.ArrayAdapterActivity">    <!--使用红色分割线-->    <ListView        android:id="@+id/lv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:divider="#f00"  //设置分割线        android:dividerHeight="3px"        android:headerDividersEnabled="false" />    <!--使用绿色分割线-->    <ListView        android:id="@+id/lv2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:divider="#0f0"        android:dividerHeight="3px"        android:headerDividersEnabled="false" /></LinearLayout>

下面是Adapter提供的列表项的数据

<?xml version="1.0" encoding="utf-8"?><TextView    android:gravity="center_horizontal"    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:padding="10dp"    android:textColor="#409"    android:shadowColor="#f0f"    android:shadowDy="3"  //设置阴影的颜色和宽度    android:shadowDx="3"    android:shadowRadius="3"    />
<?xml version="1.0" encoding="utf-8"?><CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/textview2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="20dp"    android:checkMark="@drawable/ok"    android:shadowColor="#00f"    android:shadowDx="3"    android:shadowDy="3" //设置阴影的颜色和宽度    android:shadowRadius="3"    android:textSize="24sp"></CheckedTextView>
0 0