「Android入門」Androidでlistviewを更新するサンプルプログラム

Javaコード:

package com.startnews24.android.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

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;

@Override
public 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); // footerにviewを追加

listView.setAdapter(adapter); //adapterを設定
listView.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();
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

Log.i(TAG, “firstVisibleItem="+firstVisibleItem+"\nvisibleItemCount="+
visibleItemCount+"\ntotalItemCount"+totalItemCount);

lastItem = firstVisibleItem + visibleItemCount – 1;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.i(TAG, “scrollState="+scrollState);
//総数item 更新動作を行う
if(lastItem == count && scrollState == this.SCROLL_STATE_IDLE){
Log.i(TAG, “フッター");
moreView.setVisibility(view.VISIBLE);

mHandler.sendEmptyMessage(0);

}

}
//Handlerを宣言
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
loadMoreData(); //非同期ロードを使用
adapter.notifyDataSetChanged();
moreView.setVisibility(View.GONE);

if(count > 30){
Toast.makeText(MainActivity.this, “データが存在しません!", 3000).show();
listView.removeFooterView(moreView); //footerviewを削除
}
Log.i(TAG, “多くのデータをロード");
break;
case 1:

break;
default:
break;
}
};
};

}

 

Android

Posted by arkgame