listview的資料載入,listview資料載入
效果
listview的資料載入,主要用了Scroll監聽來判斷其位置是否需要載入資訊,如果需要載入資訊就先顯示載入資料的view,然後進行資料的載入,載入完成後,設載入資料的view不可見,如果載入資料的時候資料位元null,這時就移除這個view。
listView.addFooterView(moreView); // 添加底部view(中的資料載入中...),一定要在setAdapter之前添加,否則會報錯。
listView.removeFooterView(moreView); // 移除底部(同上)
下面上個Demo,是我從網上找的。 寫個博文記錄一下這個功能,挺實用的。
public class MainActivity extends Activity implements OnScrollListener {private static final String TAG = "MainActivity";private ListView listView;private View moreView; // 載入更多頁面private SimpleAdapter adapter;private ArrayList<HashMap<String, String>> listData;private int lastItem;private int count;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.listView);moreView = getLayoutInflater().inflate(R.layout.load, null);listData = new ArrayList<HashMap<String, String>>();prepareData(); // 準備資料count = listData.size();adapter = new SimpleAdapter(this, listData, R.layout.item,new String[] { "itemText" }, new int[] { R.id.itemText });listView.addFooterView(moreView); // 添加底部view,一定要在setAdapter之前添加,否則會報錯。listView.setAdapter(adapter); // 設定adapterlistView.setOnScrollListener(this); // 設定listview的滾動事件}private void prepareData() { // 準備資料for (int i = 0; i < 10; i++) {HashMap<String, String> map = new HashMap<String, String>();map.put("itemText", "測試資料" + i);listData.add(map);}}private void loadMoreData() { // 載入更多資料count = adapter.getCount();for (int i = count; i < count + 5; i++) {HashMap<String, String> map = new HashMap<String, String>();map.put("itemText", "測試資料" + i);listData.add(map);}count = listData.size();}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {Log.i(TAG, "firstVisibleItem=" + firstVisibleItem+ "\nvisibleItemCount=" + visibleItemCount + "\ntotalItemCount"+ totalItemCount);lastItem = firstVisibleItem + visibleItemCount - 1; // 減1是因為上面加了個addFooterView}@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {Log.i(TAG, "scrollState=" + scrollState);// 下拉到空閑是,且最後一個item的數等於資料的總數時,進行更新System.out.println("lastItem:"+lastItem + "\n" + "count" +count);if (lastItem == count && scrollState == this.SCROLL_STATE_IDLE) {Log.i(TAG, "拉到最底部");moreView.setVisibility(view.VISIBLE);mHandler.sendEmptyMessage(0); //發送到Handler,執行載入資料}}// 聲明Handlerprivate Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case 0:try {Thread.sleep(400);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}loadMoreData(); // 載入更多資料,這裡可以使用非同步載入adapter.notifyDataSetChanged(); //notifyDataSetChanged()可以在修改適配器綁定的數組後,不用重新重新整理Activity,通知Activity更新ListView。moreView.setVisibility(View.GONE);if (count > 30) {Toast.makeText(MainActivity.this, "木有更多資料!", 3000).show();listView.removeFooterView(moreView); // 移除底部}Log.i(TAG, "載入更多資料");break;}};};}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" /></RelativeLayout>
listview添加資料
這個跟dataGridView有點區別,dataGridView就好像一個Excel一樣,可以直接進行增刪改的操作,rows[i]是定位到行,cells[j]定位到列。而listview與dataGridView相同的是都要預先編輯columns,而listview不需要事先與資料表中的資料行繫結,而是通過sqldatareader對象讀取的
這裡主要寫重要部分代碼:
while(datareader.read())
{
string name=(string)datareader["name"];//假定資料表中有name這個欄位,下面同上
string sex=(string)datareader["sex"];
//建立一個列表視圖項
listViewItems lvi=new listViewsItems(name);
//把建立的列表視圖項加入列表視圖中
lv.Items.Add(lvi);//其中lv為列表listview的名字
//向列表視圖中添加子項
lvi.SubItems.AddRange(new string[]{sex});
}
datareader.Close();//關閉dataReader對象
android開發listview資料重複載入
你每次載入資料的時候你存資料的list清空了嗎?重新載入資料的時候記得clear