ArrayAdapter数组适配器用于绑定格式单一的数据,数据源可以是集合或者数组
列表视图(ListView)以垂直的形式列出需要显示的列表项。
实现过程:新建适配器->添加数据源到适配器->视图加载适配器
第一种:直接用ListView组件创建
列表每一行只有一行文字
效果如图:
activity_list布局:
<?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"><ListViewandroid:id="@+id/lv_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="@android:color/holo_blue_dark"android:dividerHeight="1dp"android:entries="@array/list"></ListView><!--ListView 是列表控件,用于显示一组或许多(成百、上千)相似内容用的android:divider :表示listview的分割线android:dividerHeight:表示分割线的高度android:entries:在res/layout的string.xml中添加数组资源的名称-->
</LinearLayout>
android:entries="@array/list"></ListView><!--ListView 是列表控件,用于显示一组或许多(成百、上千)相似内容用的android:divider :表示listview的分割线android:dividerHeight:表示分割线的高度android:entries:在res/layout的string.xml中添加数组资源的名称-->
</LinearLayout>
在res/layout的string.xml中添加数组资源的名称
<resources><string name="app_name">ArrayAdapter数组适配器</string><string-array name = "list"> <!--数组资源名称为list 与layout中对应--><item>第一行</item><item>第二行</item><item>第三行</item><item>第四行</item><item>第五行</item><item>第六行</item><item>第七行</item><item>第八行</item><item>第九行</item><item>第十行</item><item>第十一行</item><item>第十二行</item><item>第十三行</item></string-array>
</resources>
<string-array name = "list"> <!--数组资源名称为list 与layout中对应--><item>第一行</item><item>第二行</item><item>第三行</item><item>第四行</item><item>第五行</item><item>第六行</item><item>第七行</item><item>第八行</item><item>第九行</item><item>第十行</item><item>第十一行</item><item>第十二行</item><item>第十三行</item></string-array>
</resources>
MainActivity.java:
package com.ied.arrayadapter;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity { ListView listView; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);listView = (ListView) findViewById(R.id.lv_list);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {String result = parent.getItemAtPosition(position).toString();//获取选择项的值Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();}});}
}
ListView listView;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);listView = (ListView) findViewById(R.id.lv_list);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {String result = parent.getItemAtPosition(position).toString();//获取选择项的值Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();}});}
}
第二种:
三个参数 (使用android自带的 android.R.layout.simple_list_item_1)
activity_list布局:
<?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"><ListViewandroid:id="@+id/lv_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="@android:color/holo_blue_dark"android:dividerHeight="1dp"></ListView>
</LinearLayout>
MainActivity.java:
package com.ied.arrayadapter;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private ListView listView;private ArrayAdapter<String> arrayAdapter;private SimpleAdapter simpleAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);listView = (ListView) findViewById(R.id.lv_list);String data[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13"};//创建数组适配器,作为数据源和列表控件联系的桥梁//第一个参数:上下文环境//第二个参数:当前列表项加载的布局文件//第三个参数:数据源arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);//listview视图加载适配器listView.setAdapter(arrayAdapter);//为列表视图中选中的项添加响应事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {String result = parent.getItemAtPosition(position).toString();//获取选择项的值Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();}});}
}
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);//listview视图加载适配器listView.setAdapter(arrayAdapter);//为列表视图中选中的项添加响应事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {String result = parent.getItemAtPosition(position).toString();//获取选择项的值Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();}});}
}
四个参数:自定义了TextView
activity_info布局:
<?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"><TextViewandroid:id="@+id/tv_info"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" />
</LinearLayout>
activity_list布局:
<?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"><ListViewandroid:id="@+id/lv_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="@android:color/holo_blue_dark"android:dividerHeight="1dp"></ListView>
</LinearLayout>
MainActivity.java:
package com.ied.arrayadapter;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private ListView listView;private ArrayAdapter<String> arrayAdapter;private ArrayList<String> data;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_info);listView = (ListView) findViewById(R.id.lv_list);initData();//数据源// String data[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13"};//创建数组适配器,作为数据源和列表控件联系的桥梁arrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_list,R.id.tv_info,data);//listview视图加载适配器listView.setAdapter(arrayAdapter);//为列表视图中选中的项添加响应事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {String result = parent.getItemAtPosition(position).toString();//获取选择项的值Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();}});}private void initData(){data = new ArrayList<String>();for (int i=0;i<13;i++){data.add("第"+(i+1)+"行");}}}
第三种:自定义ArrayAdapter数组适配器
源代码下载
密码:d4qm