我做了一个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"
/>
答案 0 :(得分:0)
像这样更改您的代码
if(testdata.moveToFirst()){
HashMap<String, String> map;
do{
map = new HashMap<String, String>();
Log.d("Cursor", testdata.getString(2));
map.put("name_en", testdata.getString(2));
data.add(map);
}while(testdata.moveToNext());
}