2011-09-30 4 views
0

J'ai un ListView et un Arrayadapter rempli de chaînes. Les données doivent être StringArray myStringsAucun article n'est affiché en ListView

Mon 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].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): [email protected] 

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 

Mise en page:

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

Répondre

1

Votre problème est dans la mise en page - la disposition linéaire par défaut à une orientation horizontale qui signifie que le bouton (qui est défini à fill_parent) remplira l'écran ne laissant aucune place pour la liste.

Si vous ajoutez l'attribut:

android:orientation="vertical" 

à votre LinearLayout alors vous devriez voir la liste.

Espérons que ça marche!

+0

THX THX THX GRAND THX – test123123