ListView中没有显示任何项目

时间:2011-09-30 07:36:33

标签: android sqlite android-layout

我有一个ListView和一个Arrayadapter充满了字符串。数据应该是StringArray myStrings

我的代码:

    public class Main extends Activity {
        private SQLiteDatabase myDB;
        final String MY_DB_NAME = "PizzaCounter";
        final String MY_DB_TABLE = "Pizza";
        private ListView lv;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Log.e("XXX", "Start");
            lv = ((ListView)findViewById(R.id.list));
            Log.e("XXX List View",lv.toString());
                onCreateDBAndDBTabled();
                Button bt = (Button)findViewById(R.id.bt_add);


                bt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        startActivityForResult(new Intent(Main.this, AddPizza.class), 1);

                    }
                });

            }
            private void onCreateDBAndDBTabled() {
                myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
                myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE
                        + " (_id integer primary key autoincrement, name varchar(100), rate integer(1), eattime datetime)"
                        +";");
            List<String> list = new ArrayList<String>();
        Cursor cursor = this.myDB.query(MY_DB_TABLE, new String[] { "name" },null,null,null,null,null,null);
              if (cursor.moveToFirst()) {
                 do {
                     Log.e("XXX", "Courser Enter: " + cursor.getString(0));
                    list.add(cursor.getString(0)); 
                    } 
                 while (cursor.moveToNext());
              }
              if (cursor != null && !cursor.isClosed()) {
                 cursor.close();
              }
              Log.e("XXX", "Coung:" + list.size());
              Log.e("XXX", "Item[0] -->" +  list.toArray()[0].toString());
              String[] mStrings = new String[] { "Nyros", "Mobile", "Android" }; 
             ArrayAdapter<String> aa = new ArrayAdapter<String>(Main.this, android.R.layout.simple_list_item_1, mStrings);
    Log.e("XXX", String.valueOf(aa.getCount()));
              lv.setAdapter(aa);
            }


The problem is that no item is shown and the list count is 3.

logcat的:

09-29 08:43:03.344: ERROR/XXX(461): Start

09-29 08:43:03.344: ERROR/XXX List View(461): android.widget.ListView@43d13918

09-29 08:43:03.384: ERROR/XXX(461): Courser Enter: EditText

09-29 08:43:03.394: ERROR/XXX(461): Count:1

09-29 08:43:03.394: ERROR/XXX(461): Item[0] -->EditText

09-29 08:43:03.394: ERROR/XXX(461): 3

布局:

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <Button android:text="hinzufügen" android:id="@+id/bt_add" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>

        <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
        </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的问题出在布局中 - 线性布局默认为水平方向,这意味着按钮(设置为fill_parent)将填满屏幕,不会留下列表视图的空间。

如果添加属性:

android:orientation="vertical"

到你的LinearLayout然后你应该看到列表。

希望有效!