标签:android java arrayadapter 适配器
Adapter常用的实现方式ArrayAdapter、simpleAdapter、SimpleCursorAdapter、BaseAdapter。
1、ArrayAdapter通常用于将数组或List集合的多个值包装成多个列表项。
arrayadapter布局文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_arrayadapter" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout></span>
<span style="font-size:18px;">public class ArrayAdapterTest extends Activity { private ListView lv_arrayadapter; private String[] str_name = new String[] { "jack", "debb", "robin", "kikt", "dog", "cat", "elep" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.arrayadapter); initView(); setData(); } private void initView() { lv_arrayadapter = (ListView) findViewById(R.id.lv_arrayadapter); //注册监听事件 lv_arrayadapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ArrayAdapterTest.this, str_name[position], Toast.LENGTH_SHORT).show(); } }); } private void setData() { //创建ArrayAdapter ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( ArrayAdapterTest.this, android.R.layout.simple_list_item_1, str_name); //绑定适配器 lv_arrayadapter.setAdapter(arrayAdapter); } }</span>
创建ArrayAdapter时指定的三个参数说明如下:
Contex::整个应用的上下文。
textViewResourceId:资源ID,代表一个TextView,用作ArrayAdapter的列表组件。
objects:列表项中的数据
转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42353249
情绪控_
标签:android java arrayadapter 适配器
原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/42353249