【问题标题】:ListActivity works fine .. but doesn't show upListActivity 工作正常..但没有出现
【发布时间】:2012-12-18 11:40:32
【问题描述】:

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

【问题讨论】:

  • 能否提供activity的xml布局文件?
  • 就是完整的布局文件。
  • 是的,这是我应用中的完整布局

标签: android android-listview listactivity simpleadapter


【解决方案1】:

像这样改变你的代码

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

}

【讨论】:

  • 即便如此他应该在列表中至少有一个元素,假设光标不为空。
  • @Luksprog 可能是Cursor 中的最后一项为空:)
  • 我已经按照你告诉我的方式做了,但是..它给了我背景图片列表而不是文字!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2022-09-27
  • 2017-12-27
  • 1970-01-01
相关资源
最近更新 更多