开发者

No item is shown in ListView

I have a ListView and a Arrayadapter filled with Strings. The data should be the StringArray myStrings

My Code:

    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].toSt开发者_JAVA百科ring());
              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

Layout:

    <?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>


Your problem is in the layout - the linear layout defaults to a horizontal orientation which means the button (which is set to fill_parent) will fill the screen leaving no room for the listview.

If you add the attribute:

android:orientation="vertical"

to your LinearLayout then you should see the list.

Hope that works!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜