0

我创建了一个 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"
    />

4

1 回答 1

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());

}
于 2012-12-18T11:45:40.870 回答