こんにちわ。ryoryoと申します。
現在辞書アプリを開発しており、表題の件ではまっております。
具体的に申しますと、ListViewに表示している検索結果が大量にある場合、
スクロールしていくと大体200行を超えたあたりでいきなり画面が閉じ、
前の画面が表示されます。画面が“閉じた”というか“落ちた”という感じです。
検索用に用意した語句は約45,000語あります。
いずれは検索結果を20件ずつ表示しListViewのaddFooterViewで配置するボタンで、続きの
20件を追加していく用に表示させたいと考えており、ひとまず大量の検索結果が出た場合、
最後まで表示されるかをチェックしたところ、今回の現象を確認した次第です。
検索結果画面に使用しているソースを簡素化した形ですが記載させて頂きます。
(ListViewに表示する内容も簡素化していますが、実際は1行に語句名以外にも種別名
や例文などいろいろ表示しています。※画像は表示していません。)
メモリの枯渇が原因か?とは思ったのですが、原因および解決方法が未だにわからない
状態です。
そもそもListViewにこんなにデータを表示させるものではないのでしょうか?
Android開発がまだ不慣れなため、説明不足や分かりずらい点などあるかと思いますが、
何卒、ご教授の程よろしくお願い致します。
■当方の開発環境
開発用PCのOS:Windows 7 Enterprise
開発プラットフォーム:Eclipse 3.5.2
アンドロイドOS:1.6以上
アプリ実行環境:Xperia(実機), Xperia(emulator), Android Virtual Devices
以下ソースです。
-
START--------------------------------------------------------------------------
【dictionaryresults.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lstResults"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:cacheColorHint="#FFFFFF"
/>
</LinearLayout>
【dictionaryresultslist.xml(各行に使用しているxml)】
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/wordLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/wordName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/theme_color"
android:textSize="24sp"
/>
<TextView
android:id="@+id/wordCode"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="0dp"
/>
</LinearLayout>
</RelativeLayout>
【DictionaryResultsController.java】
public class DictionaryResultsController extends Activity{
private ArrayList<DictionaryResultsListProperty> _ArrayList;
private DictionaryResultsListAdapter
_DictionaryResultsListAdapter;
private ListView _LstResults;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dictionaryresults);
ViewData();
}
private void ViewData(){
_LstResults = (ListView)findViewById(R.id.lstResults);
_ArrayList = new ArrayList<DictionaryResultsListProperty>();
SQLiteCursor cursor = GetHogeHoge(); // ←ここでデータベースから検索結果を取得
int resultNum = cursor.getCount();
cursor.moveToFirst();
DictionaryResultsListProperty dictionaryResultsListProperty;
for(int i=0; i <= maxCount; i++){
/*****************
/* ここでDictionaryResultsListPropertyに検索結果の値を入れる
/*****************
dictionaryResultsListProperty = new
DictionaryResultsListProperty();
dictionaryResultsListProperty.SetWordCode(cursor.getString(0));
dictionaryResultsListProperty.SetWordName(cursor.getString(1));
dictionaryResultsListProperty.SetKindCode(cursor.getString(2));
_ArrayList.add(dictionaryResultsListProperty);
cursor.moveToNext();
}
_DictionaryResultsListAdapter = new
DictionaryResultsListAdapter(this, R.layout.dictionaryresultslist,
_ArrayList);
_LstResults.setAdapter(_DictionaryResultsListAdapter);
}
}
【DictionaryResultsListProperty.java】
public class DictionaryResultsListProperty{
private String _WordCode;
private String _WordName;
private String _KindCode;
public void SetWordCode(String value){
this._WordCode = value;
}
public void SetWordName(String value){
this._WordName = value;
}
public void SetKindCode(String value){
this._KindCode = value;
}
public String GetWordCode(){
return this._WordCode;
}
public String GetWordName(){
return this._WordName;
}
public String GetKindCode(){
return this._KindCode;
}
}
【DictionaryResultsListAdapter.java】
public class DictionaryResultsListAdapter extends
ArrayAdapter<Object>{
private ArrayList<?> items;
private LayoutInflater inflater;
public DictionaryResultsListAdapter(Context context, int
textViewResourceId, ArrayList items){
super(context, textViewResourceId, items);
this.items = items;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder{
LinearLayout wordLayout;
TextView wordCode;
TextView wordName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// ビューを受け取る
View view = convertView;
ViewHolder holder;
if (view == null){
view = inflater.inflate(R.layout.dictionaryresultslist, null);
holder = new ViewHolder();
holder.wordLayout =
(LinearLayout)view.findViewById(R.id.wordLayout);
holder.wordCode = (TextView) view.findViewById(R.id.wordCode);
holder.wordName = (TextView) view.findViewById(R.id.wordName);
view.setTag(holder);
}else{
holder = (ViewHolder)view.getTag();
}
// 表示すべきデータの取得
DictionaryResultsListProperty item =
(DictionaryResultsListProperty)items.get(position);
if (item != null){
String kindCode = item.GetKindCode();
holder.wordCode.setText(item.GetWordCode());
holder.wordName.setText(item.GetWordName());
if(kindCode.equals(ConstDefine.KIND_COLOR)){
holder.wordLayout.setBackgroundColor(Color.parseColor(item.GetColorValue()));
}else{
holder.wordLayout.setBackgroundColor(Color.argb(0, 255, 255,
255));
}
}
return view;
}
}
-
END----------------------------------------------------------------------------