列表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把數據映射到ListView上的中介。
3.數據 具體的將被映射的字符串,圖片,或者基本組件。
根據列表的適配器類型,列表分爲三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最爲簡單,只能展示一行字。SimpleAdapter有最好的擴充性,可以自定義出各種效果。 SimpleCursorAdapter可以認爲是SimpleAdapter對數據庫的簡單結合,可以方面的把數據庫的內容以列表的形式展示出來。
當數據羅列過多的時候,我們需要Listview分頁顯示,此時怎麼辦呢
下面一個簡單的例子介紹一下
- package com.ideasandroid.demo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- public class listMoreTest extends Activity {
- ListView lv;
- Button btnLeft, btnRight;
- View.OnClickListener cl;
- MoreAdapter ma;
- String[] data = {
- "0","1","2","3","4","5","6","7","8","9","10",
- "11","12","13","14","15","16","17","18","19","20",
- "21","22","23","24","25","26","27","28","29","30",
- "31","32","33","34","35","36","37","38","39","40",
- "41","42","43","44","45","46","47","48","49","50",
- "51","52","53","54","55","56","57","58","59","60",
- "61","62","64","64","65","66","67","68","69","70",
- "71","72","73","74","75","76","77","78","79","80",
- "81","82","83","84","85","86","87","88","89","90",
- "91","92","93","94","95","96","97","98","99","100"
- };
- //用於顯示每列5個Item項。
- int VIEW_COUNT = 5;
- //用於顯示頁號的索引
- int index = 0;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listview);
- //加載Listview和2個Button
- initView();
- //設置ListView的Adapter
- ma = new MoreAdapter(this);
- lv.setAdapter(ma);
- //此處是雙向綁定嗎?
- cl = new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.btnLeft:
- leftView();
- break;
- case R.id.btnRight:
- rightView();
- break;
- }
- }
- };
- //添加2個Button的監聽事件。
- btnLeft.setOnClickListener(cl);
- btnRight.setOnClickListener(cl);
- //檢查2個Button是否是可用的
- checkButton();
- }
- public void initView(){
- lv = (ListView)findViewById(R.id.list);
- btnLeft = (Button)findViewById(R.id.btnLeft);
- btnRight = (Button)findViewById(R.id.btnRight);
- }
- //點擊左邊的Button,表示向前翻頁,索引值要減1.
- public void leftView(){
- index--;
- //刷新ListView裏面的數值。
- ma.notifyDataSetChanged();
- //檢查Button是否可用。
- checkButton();
- }
- //點擊右邊的Button,表示向後翻頁,索引值要加1.
- public void rightView(){
- index++;
- //刷新ListView裏面的數值。
- ma.notifyDataSetChanged();
- //檢查Button是否可用。
- checkButton();
- }
- public void checkButton(){
- //索引值小於等於0,表示不能向前翻頁了,以經到了第一頁了。 //將向前翻頁的按鈕設爲不可用。
- if(index <=0){
- btnLeft.setEnabled(false);
- }
- //值的長度減去前幾頁的長度,剩下的就是這一頁的長度,如果這一頁的長度比View_Count小,表示這是最後的一頁了,後面在沒有了。
- //將向後翻頁的按鈕設爲不可用。
- else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){
- btnRight.setEnabled(false);
- }
- //否則將2個按鈕都設爲可用的。
- else {
- btnLeft.setEnabled(true);
- btnRight.setEnabled(true);
- }
- }
- //ListView的Adapter,這個是關鍵的導致可以分頁的根本原因。
- public class MoreAdapter extends BaseAdapter {
- Activity activity;
- public MoreAdapter(Activity a){
- activity = a;
- }
- //設置每一頁的長度,默認的是View_Count的值。
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- //return data.length
- //ori表示到目前爲止的前幾頁的總共的個數。
- int ori = VIEW_COUNT * index;
- //值的總個數-前幾頁的個數就是這一頁要顯示的個數,如果比默認的值小,說明這是最後一頁,只需顯示這麼多就可以了
- if(data.length - ori < VIEW_COUNT ){
- return data.length - ori;
- }
- //如果比默認的值還要大,說明一頁顯示不完,還要用換一頁顯示,這一頁用默認的值顯示滿就可以了。
- else {
- return VIEW_COUNT;
- }
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- //return addTestView(position);
- TextView tv = new TextView(activity);
- tv.setGravity(Gravity.CENTER);
- //TextView要顯示的是當前的位置+前幾頁已經顯示的位置個數的對應的位置上的值。
- tv.setText(data[position+index*VIEW_COUNT]);
- return tv;
- }
- }
- }
package com.ideasandroid.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class listMoreTest extends Activity {
ListView lv;
Button btnLeft, btnRight;
View.OnClickListener cl;
MoreAdapter ma;
String[] data = {
"0","1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33","34","35","36","37","38","39","40",
"41","42","43","44","45","46","47","48","49","50",
"51","52","53","54","55","56","57","58","59","60",
"61","62","64","64","65","66","67","68","69","70",
"71","72","73","74","75","76","77","78","79","80",
"81","82","83","84","85","86","87","88","89","90",
"91","92","93","94","95","96","97","98","99","100"
};
//用於顯示每列5個Item項。
int VIEW_COUNT = 5;
//用於顯示頁號的索引
int index = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
//加載Listview和2個Button
initView();
//設置ListView的Adapter
ma = new MoreAdapter(this);
lv.setAdapter(ma);
//此處是雙向綁定嗎?
cl = new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnLeft:
leftView();
break;
case R.id.btnRight:
rightView();
break;
}
}
};
//添加2個Button的監聽事件。
btnLeft.setOnClickListener(cl);
btnRight.setOnClickListener(cl);
//檢查2個Button是否是可用的
checkButton();
}
public void initView(){
lv = (ListView)findViewById(R.id.list);
btnLeft = (Button)findViewById(R.id.btnLeft);
btnRight = (Button)findViewById(R.id.btnRight);
}
//點擊左邊的Button,表示向前翻頁,索引值要減1.
public void leftView(){
index--;
//刷新ListView裏面的數值。
ma.notifyDataSetChanged();
//檢查Button是否可用。
checkButton();
}
//點擊右邊的Button,表示向後翻頁,索引值要加1.
public void rightView(){
index++;
//刷新ListView裏面的數值。
ma.notifyDataSetChanged();
//檢查Button是否可用。
checkButton();
}
public void checkButton(){
//索引值小於等於0,表示不能向前翻頁了,以經到了第一頁了。 //將向前翻頁的按鈕設爲不可用。
if(index <=0){
btnLeft.setEnabled(false);
}
//值的長度減去前幾頁的長度,剩下的就是這一頁的長度,如果這一頁的長度比View_Count小,表示這是最後的一頁了,後面在沒有了。
//將向後翻頁的按鈕設爲不可用。
else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){
btnRight.setEnabled(false);
}
//否則將2個按鈕都設爲可用的。
else {
btnLeft.setEnabled(true);
btnRight.setEnabled(true);
}
}
//ListView的Adapter,這個是關鍵的導致可以分頁的根本原因。
public class MoreAdapter extends BaseAdapter {
Activity activity;
public MoreAdapter(Activity a){
activity = a;
}
//設置每一頁的長度,默認的是View_Count的值。
@Override
public int getCount() {
// TODO Auto-generated method stub
//return data.length
//ori表示到目前爲止的前幾頁的總共的個數。
int ori = VIEW_COUNT * index;
//值的總個數-前幾頁的個數就是這一頁要顯示的個數,如果比默認的值小,說明這是最後一頁,只需顯示這麼多就可以了
if(data.length - ori < VIEW_COUNT ){
return data.length - ori;
}
//如果比默認的值還要大,說明一頁顯示不完,還要用換一頁顯示,這一頁用默認的值顯示滿就可以了。
else {
return VIEW_COUNT;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return addTestView(position);
TextView tv = new TextView(activity);
tv.setGravity(Gravity.CENTER);
//TextView要顯示的是當前的位置+前幾頁已經顯示的位置個數的對應的位置上的值。
tv.setText(data[position+index*VIEW_COUNT]);
return tv;
}
}
}