データベースからカーソルにデータを取得する ListActivity を作成し、それらを ArrayLisy> に追加してから、Simple adatper に投稿しました。
すべて正常に動作していますが、空の画面が表示されます。
コードは次のとおりです。
public class Cities extends ListActivity {
ListView listV ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.only_text_listview);
//getting the database opened from the adapter
CitiesAdapter mDbHelper = new CitiesAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
// getting cursor from data base
Cursor testdata = mDbHelper.getTestData();
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
if(testdata.moveToFirst()){
HashMap<String, String> map = new HashMap<String, String>();
do{
Log.d("Cursor", testdata.getString(2));
map.put("name_en", testdata.getString(2));
}while(testdata.moveToNext());
data.add(map);
}
String[] from = {"name_en"};
int[] to = {R.id.only_text_lv};
SimpleAdapter SA = new SimpleAdapter(this, data, R.layout.only_text_listview , from, to);
this.setListAdapter(SA);
mDbHelper.close();
}
xml ファイル:
only_text_list.xml
<ListView
android:id ="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>
only_text_lv.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id ="@+id/only_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>