首页 > 代码库 > 学习笔记ListView初识

学习笔记ListView初识

  1.ListView的基本使用

作用:android系统中显示列表的控件

  2.两种适配器(ArrayAdapter、simpleAdapter)

数据适配器 是连接数据源和视图界面的桥梁
作用:把复杂的数据(数组、链表、数据库、集合等)填充到指定视图界面上

ArrayAdapter:用于绑定格式单一数据
数据源:集合或者数组
SimpleAdapter:用于绑定格式复杂的数据
数据源:只能是特定泛型的集合
实现过程:新建适配器->添加数据源到适配器->视图加载适配器

使用ArrayAdapter:

package com.drumbass.listviewtest;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity1 extends Activity {    private ListView listView;    private ArrayAdapter<String> mArrayAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView1);        String mdata[] = { "DrumBass1", "DrumBass2", "DrumBass3", "DrumBass4",                "DrumBass5" };        mArrayAdapter = new ArrayAdapter<String>(this,                android.R.layout.simple_list_item_1, mdata);        listView.setAdapter(mArrayAdapter);    }}

技术分享

使用simpleAdapter:

 1 package com.drumbass.listviewtest; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import android.app.Activity; 9 import android.os.Bundle;10 import android.widget.ListView;11 import android.widget.SimpleAdapter;12 13 public class MainActivity2 extends Activity {14     private ListView listView;15     private SimpleAdapter mSimpleAdapter;16     private List<Map<String, Object>> mDataList;17 18     @Override19     protected void onCreate(Bundle savedInstanceState) {20         super.onCreate(savedInstanceState);21         setContentView(R.layout.activity_main);22         listView = (ListView) findViewById(R.id.listView1);23         mDataList = new ArrayList<Map<String, Object>>();24         mSimpleAdapter = new SimpleAdapter(this, getData(), R.layout.item,25                 new String[] { "pic", "text" },26                 new int[] { R.id.pic, R.id.text });27         listView.setAdapter(mSimpleAdapter);28     }29     private List<Map<String, Object>> getData(){30         for (int i = 0; i < 20; i++) {31             Map<String,Object> map = new HashMap<String,Object>();32             map.put("pic", R.drawable.ic_launcher);33             map.put("text", "DrumBass"+i);34             mDataList.add(map);35         }36         return mDataList;37     }38 39 }

自定义xml文件:item.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout  3     xmlns:android="http://schemas.android.com/apk/res/android" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:orientation="horizontal" > 7     <ImageView 8         android:layout_height="wrap_content" 9         android:layout_width="wrap_content"10         android:src="@drawable/ic_launcher"11         android:id="@+id/pic"12         />13     <TextView 14         android:id="@+id/text"15         android:layout_width="wrap_content"16         android:layout_height="wrap_content"17         android:text="DrumBass"18         android:gravity="center"19         />20 21 </LinearLayout>

运行结果:

技术分享

  3.两种监听器(OnScrollingListener、OnItemClickListener)

  4.适配器数据刷新(notifyDataChanged)

 1 package com.drumbass.listviewtest; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import android.app.Activity; 9 import android.os.Bundle;10 import android.util.Log;11 import android.view.View;12 import android.widget.AbsListView;13 import android.widget.AbsListView.OnScrollListener;14 import android.widget.AdapterView;15 import android.widget.AdapterView.OnItemClickListener;16 import android.widget.ListView;17 import android.widget.SimpleAdapter;18 import android.widget.Toast;19 20 public class MainActivity2 extends Activity implements OnItemClickListener,21         OnScrollListener {22     private ListView listView;23     private SimpleAdapter mSimpleAdapter;24     private List<Map<String, Object>> mDataList;25 26     @Override27     protected void onCreate(Bundle savedInstanceState) {28         super.onCreate(savedInstanceState);29         setContentView(R.layout.activity_main);30         listView = (ListView) findViewById(R.id.listView1);31         mDataList = new ArrayList<Map<String, Object>>();32         mSimpleAdapter = new SimpleAdapter(this, getData(), R.layout.item,33                 new String[] { "pic", "text" },34                 new int[] { R.id.pic, R.id.text });35         listView.setAdapter(mSimpleAdapter);36         listView.setOnItemClickListener(this);37         listView.setOnScrollListener(this);38     }39 40     private List<Map<String, Object>> getData() {41         for (int i = 0; i < 20; i++) {42             Map<String, Object> map = new HashMap<String, Object>();43             map.put("pic", R.drawable.ic_launcher);44             map.put("text", "DrumBass" + i);45             mDataList.add(map);46         }47         return mDataList;48     }49 50     @Override51     public void onScrollStateChanged(AbsListView view, int scrollState) {52         switch (scrollState) {53         case SCROLL_STATE_FLING:54             Log.i("Main", "用户在手指离开之前,用力滑动,视图还在滚动");55             Map<String, Object> map = new HashMap<String, Object>();56             map.put("pic", R.drawable.ic_launcher);57             map.put("text", "TR");58             mDataList.add(map);59             mSimpleAdapter.notifyDataSetChanged();60             break;61         case SCROLL_STATE_IDLE:62             Log.i("Main", "视图停止滑动");63             break;64         case SCROLL_STATE_TOUCH_SCROLL:65             Log.i("Main", "视图在滚动");66             break;67         }68     }69 70     @Override71     public void onScroll(AbsListView view, int firstVisibleItem,72             int visibleItemCount, int totalItemCount) {73 74     }75 76     @Override77     public void onItemClick(AdapterView<?> parent, View view, int position,78             long id) {79         String text = listView.getItemAtPosition(position) + "";80         Toast.makeText(this, "position" + position + "text" + text,81                 Toast.LENGTH_SHORT).show();82     }83 84 }

 

技术分享
 

 

终于写完了,虽然都只是笔记,但是花了很多时间。也许是我的学习方法错了? 是不是应该学完后练练再来写? 那样也许效率会高很多,下次试试。
 

 

学习笔记ListView初识