Android手机操作系统中,有许多应用比较复杂,对于初学者来说还需要在实践中去加深对此的理解。在Android ListView的使用较为复杂一点,也就是配置其Adapter,Adapter有几种,有ArrayAdapter,SimpleAdapter等,首先要生成一个ListView(当然可以使用ListActivity,此Activity整合了ListView)然后用Adapter来设定ListView的显示数据及布局方式,然后再来响应OnItemClick 事件,或者在ListActivity改写onListItemClick 响应事件函数。看如下代码演示了使用ListActivity:
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class Test extends ListActivity {
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- super.onListItemClick(l, v, position, id);
- this.setTitle(this.mModelData.get(position).get("type").toString());
- }
- SimpleAdapter adapter = null;
- private ArrayList
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initModelData();
- adapter = new SimpleAdapter
(this, mModelData, android.R.layout.two_line_list_item,
new String[]{"name"}, new int[]{android.R.id.text1});- this.setListAdapter(adapter);
- //setContentView(R.layout.main);
- }
- public void initModelData()
- {
- mModelData = new ArrayList
- Map
item = new HashMap (); - item.put("name", "Linux");item.put("type", "OS");
- mModelData.add(item);
- Map
item2 = new HashMap (); - item2.put("name", "Android");item2.put("type", "Platform");
- mModelData.add(item2);
- Map
item3 = new HashMap (); - item3.put("name", "Tomato");item3.put("type", "Fruit");
- mModelData.add(item3);
- }
- }
下面代码显示了,使用Android ListView + Activity:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.AdapterView.OnItemClickListener;
- public class TestStringList extends Activity implements
OnItemClickListener {- @Override
- public void onItemClick(AdapterView
- > arg0,
- View arg1, int arg2, long arg3) {
- this.setTitle("You Click Item:" + String.valueOf(arg2));
- }
- private ListView mListView = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- mListView = new ListView(this);
- mListView.setOnItemClickListener(this);
- ArrayAdapter
adapter = new ArrayAdapter - (this,android.R.layout.simple_list_item_checked,
- new String[]{"ItemA", "ItemB", "ItemC"});
- mListView.setAdapter(adapter);
- this.setContentView(mListView);
- }
- }
Android ListView的相关应用就为大家介绍到这里。
【编辑推荐】
网站栏目:AndroidListView复杂使用方式简单理解
本文地址:http://www.shufengxianlan.com/qtweb/news9/331359.html
成都网站建设公司_创新互联,为您提供网站设计、手机网站建设、网站改版、动态网站、做网站、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联