ListActivity未在屏幕上显示,只能看到空白的白色屏幕

时间:2014-10-07 00:27:21

标签: android android-listview

我必须做一些愚蠢的事情并且缺少一些关键部分。请帮我调试这个简单的代码,用于从数据库中呈现ListActivity

public class ExAct1 extends ListActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //I'm trying to use default view but using my own <ListView> also doesn't work
    //setContentView(R.layout.exp_list_act1); 

    From query = new Select().from(CategoryTable.class); //ActiveAndroid Library
    Cursor cursor = Cache.openDatabase().rawQuery(query.toSql(), query.getArguments());
    //startManagingCursor(cursor); //commenting or uncommenting has no effect

    this.setListAdapter(new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            cursor,
            new String[] {"_id"}, //tried other column name from my table - no effect
            new int[] {android.R.id.text1},
            0));

    //these work - just to test Cursor is valid and has data
    Log.e(TAG, "Cursor Row Count: " + cursor.getCount()); // 3 rows
    Log.e(TAG, "Cursor Col Count: " + cursor.getColumnCount()); // 3 cols
  }
}

老实说,这是我的整个班级代码。我可以在屏幕上看到活动,标题栏中的名称与该活动的清单中定义的名称相同 - 但在标题下方我只看到空白的白色屏幕。

2 个答案:

答案 0 :(得分:0)

您需要取消注释该行

//的setContentView(R.layout.exp_list_act1);

您的xml布局文件必须包含id为“@android:id / list”

的列表视图

答案 1 :(得分:0)

ActiveAndroid使用&#34; Id&#34;作为默认的id列名。它可以更改为&#34; _id&#34;,但如果您不记得这样做,那么这可能是您的问题。

若是,请尝试这样做:

setListAdapter(new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1,
        cursor,
        new String[] {"Id"},
        new int[] {android.R.id.text1},
        0));

或者,您可以像这样更改CategoryTable类中的id列名称:

@Table(name = "category", id = BaseColumns._ID)
public class CategoryTable extends Model {...}
相关问题