listView.setOnItemClickListener(listener) --- 将监听器注册到ListView中
实例代码:
ArrayAdapter与ListView
package com.example.androidbasicdemo1; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; /** * ArrayList<类型>(context,int itemLayout, data) * context:上下文环境 * itemLayout:自定义的每一项要显示的Layout * data:要显示的数据 * * 例子: * ArrayAdapter<String> adapter = new ArrayAdapter<String>(ArrayAdapterListViewActivity.this, android.R.layout.simple_list_item_1, data); android.R.layout.simple_list_item_1 --- 使用Android自带的 每一项Listview显示布局 -- 实际应用中常常自定义此item布局 * @author JayHe * */ public class ArrayAdapterListViewActivity extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_array_adapter_list_view); //从xml布局文件中获得ListView对象 listView = (ListView) findViewById(R.id.arrayadapterlistView1); //定义要显示的数据 String[] data = getResources().getStringArray(R.array.items_country); //定义适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>(ArrayAdapterListViewActivity.this, android.R.layout.simple_list_item_1, data); //为ListView添加适配器 listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.array_adapter_list_view, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
SimpleAdapter与ListView
package com.example.androidbasicdemo1; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class SimpleAdapterListViewActivity extends Activity { private List<Map<String, Object>> data; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_adapter_list_view); listView = (ListView) findViewById(R.id.simpleadapterlistView1); SimpleAdapter adapter = new SimpleAdapter(SimpleAdapterListViewActivity.this, prepareData(), R.layout.slef_list_item1,new String[]{"姓名","性别"}, new int[]{R.id.selflistview1textView1,R.id.selflistview1textView2}); listView.setAdapter(adapter); OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { setTitle(parent.getItemAtPosition(position).toString()); } }; listView.setOnItemClickListener(listener); } private List<Map<String, Object>> prepareData(){ data = new ArrayList<Map<String, Object>>(); Map<String, Object> item; item = new HashMap<String, Object>(); item.put("姓名", "吴欢小盆友"); item.put("性别", "女"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "冯荣小朋友"); item.put("性别", "女"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "夏明"); item.put("性别", "男"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "小华"); item.put("性别", "男"); data.add(item); return data; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.simple_adapter_list_view, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Android开发详解之ListView详解一,布布扣,bubuko.com
原文地址:http://blog.csdn.net/he90227/article/details/25805195